2013年10月31日星期四

Fundamental questions about a thread , a large head , depressed

This is an example from the Internet to find
class ThreadA
{
public static void main (String [] args)
{
ThreadB b = new ThreadB ();
b.start ();
System.out.println ("b is start ....");
synchronized (b)
{
try
{
System.out.println ("Waiting for b to complete ...");
b.wait ();
System.out.println ("Completed.Now back to main thread");
}
catch (InterruptedException e)
{
}
}
System.out.println ("Total is:" + b.total);
}
}

class ThreadB extends Thread
{
int total;
public void run ()
{
synchronized (this)
{
System.out.println ("ThreadB is running ..");
for (int i = 0; i <100; i + +)
{
total + = i;
System.out.println ("total is" + total);
}
notify ();
System.out.println ("ThreadB is finish ..");
}
}
}

ThreadA run the main program , the following is the result of the majority
b is start ....
ThreadB is running ..
total is 0
total is 1
total is 3
total is 6
............ ( this is the middle of the omitted results )
ThreadB is finish ..
Waiting for b to complete ...
Completed.Now back to main thread
Total is: 4950

But after running it several times , and sometimes the results will appear as though it is a small probability
b is start ....
ThreadB is running ..
total is 0
total is 1
total is 3
............ ( this is the middle of the omitted results )
ThreadB is finish ..
Waiting for b to complete ...
program is in an infinite wait state , unless I manually end

My question now is , according to the output of each ThreadB basically be able to grab the lock , and then to perform, so I think
should be the main thread each time an infinite wait state fishes , but the actual result is actually able to perform most complete , and that the wait method here , what role ? feel there is no work , wait not to say, the object of the thread holds the lock release the right, and then thread into a collection of objects waiting for you ? ThreadA here does not hold the object b is the main thread do ? Once we reach the wait method , the main thread should not hang you, he To continue, shall perform such other places notify you, but now than he first performed notify Yeah, that never have the thread wakes him die , how he will continue to enforce it ?

Please adequate guidance
------ Solution -------------------------------- ------------
My question now is , according to the output of each ThreadB basically be able to grab the lock , and then to perform, so I think
should be the main thread each time an infinite wait state fishes

========================
ThreadB are not always grab the lock ah, you say that basically it. And do not print so much, you sleep it. See clearly point .

public class ThreadA {
public static void main(String[] args) {
ThreadB b = new ThreadB();
b.start();
System.out.println("b is start....");
synchronized (b) {
try {
System.out.println("Waiting for b to complete...");
System.out.println("before wait...");
b.wait();
System.out.println("after wait...");
System.out.println("Completed.Now back to main thread");
} catch (InterruptedException e) {
}
}
System.out.println("Total is :" + b.total);
}
}

class ThreadB extends Thread {
int total;

public void run() {
synchronized (this) {
System.out.println("ThreadB is running..");
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
total += i;
System.out.println("total is " + total);
}
notify();
System.out.println("ThreadB is finish..");
}
}
}

two cases:
1, the main thread to run in ThreadB execution is performed prior to the sync block inside. That this is the case :
b is start ....
Waiting for b to complete ...
before wait ...
ThreadB is running ..
total is 0
total is 1
total is 3
total is 6
total is 10
total is 15
total is 21
total is 28
total is 36
total is 45
ThreadB is finish ..
after wait ...
Completed.Now back to main thread
Total is: 45

1, the main thread to run in ThreadB execution is performed after the sync block inside, and this on an infinite wait.

b is start ....
ThreadB is running ..
total is 0
total is 1
total is 3
total is 6
total is 10
total is 15
total is 21
total is 28
total is 36
total is 45
ThreadB is finish ..
Waiting for b to complete ...
before wait ...

------ For reference only ---------------------------------- -----
for this one
Once we reach the wait method , the main thread should not hang you, he would like to continue, other places may not perform notify you, but now than he first performed notify Yeah, that thread will never wake up he ah , how he will continue to enforce it ?

Waiting for b to complete ..
b.wait () ;/ / wait up here . Run it back there ?
System.out.println ("Completed.Now back to main thread");
------ For reference only ------------ ---------------------------
withheld good. You do not see it that fast execution . But it is still performed first and then the inside of the A to B .
------ For reference only -------------------------------------- -
Thank you ! ! ! really is such a thing

没有评论:

发表评论