java - How to do thread interacting? -


i want use multithreading (low -level threading) running problem. problem because of @ least wait method called 1 thread , notifyall called thread problem time run program seems me notifyall called before wait "will wait forever".

my code below :

public class reader extends thread {      calculator c;      public reader(calculator cal) {         c=cal;     }      public void run (){         synchronized(c) {             try {                 system.out.println("waiting calculation");                 c.wait();             } catch (interruptedexception ex) {}             system.out.println("total is:" +c.total);               }            }        public static void main (string[] a) {          calculator calculator=new calculator();          new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();     } }  class calculator implements runnable {      int total;      public void run()  {         synchronized(this) {                 for(int i=0;i<100;i++) {                 total=total+i;             }                    notifyall();             }     } } 

what here output 5 times in row waiting calculation never reach statement "the total total.

i trying figure out how solve problem still not finding solution. if has clue appreciate lot.

thanks in advance

you're not executing calculator runnable, code have won't calculate total or notify anything. start new thread using calculator:

new thread(calculator).start(); 

following line initialize calculator local variable. let calculator start working while main thread starting readers.

still it's calculator may finish before readers can start waiting, , readers end waiting forever notification never come. make condition variable, in case adding boolean flag calculator, initialize false , set true once it's done calculating (by notifyall, before leaving synchronized block). have readers wait in loop:

synchronized(c) {     try {         system.out.println("waiting calculation");         while (!c.finished) {             c.wait();         }     } catch (interruptedexception ex) {}     system.out.println("total is:" +c.total); } 

this way, in case calculator finishes before readers start, readers tell condition variable calculator finished , won't wait.

it's practice call wait method in loop because

a) when thread receives notification doesn't have lock, in time between when thread notified , time reacquires lock state of system change due other threads' actions (not case in example), and

b) can't rely on termination of wait meaning thread received notification.

with above changes program finishes output:

waiting calculation total is:4950 waiting calculation total is:4950 waiting calculation total is:4950 waiting calculation total is:4950 waiting calculation total is:4950 

here's complete revised code:

public class reader extends thread {      calculator c;      public reader(calculator cal) {         c=cal;     }      public void run (){         synchronized(c) {             try {                 system.out.println("waiting calculation");                 while (!c.finished) {                     c.wait();                 }             } catch (interruptedexception ex) {}             system.out.println("total is:" +c.total);               }            }        public static void main (string[] a) {          calculator calculator=new calculator();         new thread(calculator).start();          new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();         new reader(calculator).start();     } }  class calculator implements runnable {      int total;     boolean finished;      public void run()  {         synchronized(this) {                 finished = false;             for(int i=0;i<100;i++) {                 total=total+i;             }                    notifyall();                 finished = true;         }     } }  

Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -