Home > Back-end >  [simple multithreading issues] three threads respectively to print 5 A, B, C, print order for ABC...
[simple multithreading issues] three threads respectively to print 5 A, B, C, print order for ABC...

Time:09-29

My question is as follows:
Why the run () method of the statement is getCount () & lt; 13?
Can be seen from the output of the program finally did not end when the count value is 15, but the count value from 13, the run () method will not be called for,
Or is the operation result and wait () and notifyAll () method is used, is a great god can you explain it?

A test class:
Public class Test {
Public static void main (String [] args) {

//create a PrintCopy class object
PrintCopy PC=new PrintCopy ();

//will PrintCopy class object as a parameter to a Print
The Print Print=new Print (PC);

//3 new threads, and at the same time, named "A", "B", "C"
Thread the t1=new Thread (print, "A");
Thread t2=new Thread (print, "B");
The Thread t3=new Thread (print, "C");

//start the thread
T1. Start ();
T2. Start ();
T3. The start ();
}

}

Second, printing categories:
Public class PrintCopy
{
Private int flag=1;
Private int count=0;

Public int getCount () {
return count;
}

Public synchronized void printA () {
While (flag!=1) {
Try {
wait();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
System. Out. Print (Thread. CurrentThread (). The getName ());
Flag=2;
count++;
System. The out. Print (count);
NotifyAll ();
}

Public synchronized void printB () {
While (flag!=2) {
Try {
wait();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
System. Out. Print (Thread. CurrentThread (). The getName ());
Flag=3;
count++;
System. The out. Print (count);
NotifyAll ();
}

Public synchronized void printC () {
While (flag! {
=3)Try {
wait();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
System. Out. Print (Thread. CurrentThread (). The getName ());
Flag=1;
count++;
System. The out. Print (count);
NotifyAll ();
}

}

The class Print implements Runnable {
Private PrintCopy PC;
Public Print (PrintCopy PC) {
This. PC=PC;
}
Public void the run () {
//print control number
While (PC) getCount () & lt; 13) {
If (Thread. CurrentThread (). The getName () equals (" A ")) {
PC. PrintA ();
}
Else if (Thread. CurrentThread (). The getName () equals (" B ")) {
PC. PrintB ();
}
Else if (Thread. CurrentThread (). The getName () equals (" C ")) {
PC. The printC ();
}
}
}
}
3, printing the results
A1B2C3A4B5C6A7B8C9A10B11C12A13B14C15

CodePudding user response:

The
refer to the original poster Yizhinailu response:
my question is as follows:
Why the run () method of the statement is getCount () & lt; 13?
Can be seen from the output of the program finally did not end when the count value is 15, but the count value from 13, the run () method will not be called for,
Or is the operation result and wait () and notifyAll () method is used, is a great god can you explain it?


PrintA because it is over, printB printC is parallel and lock, so the count of thread A judgment is actually on the wheel A executed when the count value, the same B, C, has said the last 1 round A thread getCount values of 10 and has reached the awaited awakening, thread B getCount value of 11 and has reached the awaited awakening, C thread getCount for value 12 then to wait, next 13 out of circulation, A count variable count change after 14 out of circulation, B C count change after 15 out of circulation,

CodePudding user response:

Because when you spin the count without judgment

CodePudding user response:

First of all, you know, there are three threads (not counting the main thread and gc), respectively is ABC
When the count=12, while just finished printing c12, starting from the analysis (PC. GetCount () & lt; 13)
For B threads run (PC) printB () method, stuck in a wait ())
For C threads run (PC) printC () method, stuck in a wait ())
Run for A thread (PC. PrintA () method, the flag=1, count++, print A13, flag set to 2, and finally notifyAll ())
While at this time (PC. GetCount () & lt; 13) was set up, no one thread go here will all come to an end, but the BC thread right now just wake up, wake up at the code wait, so it will also print B14, C15, execution of the PC, printB (), PC. The printC (), then will go to the while whether the count is less than 13, of course, at this point is not set up, so will be terminated,


But I feel this way, will not guarantee that can output to 15, 13,14,15 may have to stop, you can try to run a few times more

CodePudding user response:

My idea is the same with you, but to see the upstairs brother explanation, just feel a little hard to understand,
Run a lot of times, it is finally in the 15 to stop here,

CodePudding user response:

reference 4 floor Yizhinailu response:
my idea and you are same, but see upstairs brother explanation, just feel a little hard to understand,
Run a lot of times, it is finally in the 15 to stop here,


You can try to put you in the run method B, C thread part of the execution of the print sleep a short while later, see won't print the results are different

CodePudding user response:

reference 4 floor Yizhinailu response:
my idea and you are same, but see upstairs brother explanation, just feel a little hard to understand,
Run a lot of times, it is finally in the 15 to stop here,


Because the speed of execution from the wait () to count++, there are 2 before other statements to be executed, and the run () because if the else nested inside, so wake up the statement executes immediately after getCount () & lt; 13, so generally normal count order will happen to be upset,