Multithreading (java) I'm currently making an IRC Bot that I plan on connecting to multiple channels. each user join will spawn a new thread and that thread will be responsible with all user communications to that channel. I am having a problem trying to signal to all threads that the user wants to quit the program. I tried having a boolean variable that just toggles when the user types /quit but I'm having difficulties with it. Do threads share primitive variables amoungst each other?
about your problem.. im not yet good in java programming,thats why, but since im getting related to what you are trying to know ill give some of my knowledge about that, im currently studying about defining your own classes in java and luckily that part is what am i studying right now about sharing of variables in different classes... maybe you should declare all the variables as public first so any objects or classes that will invoke those variables can be accesed. I defined a method and i declared public one of its variables .. and thats when I noticed that i can accessed it .. even if i called that variable in another predefined classes that i make., in a proper way. If you want to make all your objects accessable ofcourse you should declare it as public. and declare it also as static so it can be accessed or called also by a "CLASS NAME" . If those variables are from a primitive type or data type avoid using "VOID" type of return examples: String,int.long,double.. etc... i hop this will help you a little bit. BOOM!
Try this Thread myThread = new Thread(new Runnable(){ @Override public void run(){ while(!Thread.currentThread().isInterrupted()){ //do something here try{ Thread.sleep(TIME_TO_SLEEP); }catch(InterruptedException e){ Thread.currentThread().interrupt(); //send interrupt } } } }); myThread.start(); If you reference myThread then to stop myThread you would invoke myThread.interrupt(). all threads should end.
Join our real-time social learning platform and learn together with your friends!