5
I’m studying Threads Java and its resources and I came across a question.
I have the following classes in my program:
public class Main {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();
synchronized (b) {
try {
System.out.println("Waiting for complete b...");
b.wait();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Total: " + b.total);
}
}
}
ThreadB
:
public class ThreadB extends Thread {
int total;
@Override
public void run() {
synchronized (this) {
for (int i = 0; i < 200; i++) {
total += i;
}
notify(); //notifyAll();
while(true) {
//Do something
}
}
}
}
Running this program I cannot get the expected behavior of the command notify()
when I add the code snippet below in ThreadB
:
while(true) {
//Do something
}
See the exits with and without the quoted passage
I’ll just have to wait it out ThreadB
even Main
notified that the total is already calculated?
There is something that can be done to get total without ThreadB
finalize?
Before I got here I consulted this one doubt with similar subject matter from another user.
Really this is not necessary, I will edit. Thanks for the remark.
– Eduardo
The example I mentioned is an abstraction of the project I am developing. Here and here . In the project I have Thread of socket clients whenever I accept a connection, and basically within the while of the thread, I have a socket manager. All I need to do is get some attributes from the thread created as soon as notify()
– Eduardo