Please bosses to reassure thank you
Phenomenon: 1 thread.sleep in synchronized code block 10 seconds of sleep
Public class ImplementsRunnable implements Runnable {
Private int ticketNums=10;
Private Boolean flag=true;
@ Override
Public void the run () {
While (flag) {
//
Try {
Buy ();
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
Public synchronized void buy () throws InterruptedException {
If (ticketNums & lt;=0) {
flag=false;
return ;
}
//sleep 10 seconds
Thread.sleep (10000);
System. The out. Println (Thread. CurrentThread (). The getName () + "won the first" + ticketNums - + "ticket");
}
Public static void main (String [] args) {
ImplementsRunnable ImplementsRunnable=new ImplementsRunnable ();
Thread the Thread=new Thread (implementsRunnable, "small 2");
Thread thread1=new Thread (implementsRunnable, "small three");
Thread thread2=new Thread (implementsRunnable, "four");
Thread thread3=new Thread (implementsRunnable, "small five");
Thread. The start ();
Thread1. Start ();
Thread2. Start ();
Thread3 start ();
//print thread state
While (true) {
If ((thread. GetState ()==thread. State. The TERMINATED) | |
(thread1. GetState ()==Thread. State. The TERMINATED) | |
(thread2. GetState ()==Thread. State. The TERMINATED) | |
(thread3. GetState ()==Thread. State. The TERMINATED)
) {
break;
}
System. The out. Println (" thread="+ thread. GetState ());
System. The out. Println (" thread1="+ thread1. GetState ());
System. The out. Println (" thread2="+ thread2. GetState ());
System. The out. Println (" thread3="+ thread3. GetState ());
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//a print wait 10 seconds
Try {
Thread.sleep (10000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}
Phenomenon is a result:
Phenomenon. 2: Thread. Sleep method in the run method perform
Public class ImplementsRunnable implements Runnable {
Private int ticketNums=10;
Private Boolean flag=true;
@ Override
Public void the run () {
While (flag) {
//
Try {
Buy ();
//sleep 10 seconds
Thread.sleep (10000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
Public synchronized void buy () {
If (ticketNums & lt;=0) {
flag=false;
return ;
}
System. The out. Println (Thread. CurrentThread (). The getName () + "won the first" + ticketNums - + "ticket");
}
Public static void main (String [] args) {
ImplementsRunnable ImplementsRunnable=new ImplementsRunnable ();
Thread the Thread=new Thread (implementsRunnable, "small 2");
Thread thread1=new Thread (implementsRunnable, "small three");
Thread thread2=new Thread (implementsRunnable, "four");
Thread thread3=new Thread (implementsRunnable, "small five");
Thread. The start ();
Thread1. Start ();
Thread2. Start ();
Thread3 start ();
//print thread state
While (true) {
If ((thread. GetState ()==thread. State. The TERMINATED) | |
(thread1. GetState ()==Thread. State. The TERMINATED) | |
(thread2. GetState ()==Thread. State. The TERMINATED) | |
(thread3. GetState ()==Thread. State. The TERMINATED)
) {
break;
}
System. The out. Println (thread. The getName () + "thread=" + thread. The getState ());
System. Out. Println (thread1. GetName () + "thread=" + thread1 getState ());
System. Out. Println (thread2. GetName () + "thread=" + thread2 getState ());
System. Out. Println (thread3. GetName () + "thread=" + thread3 getState ());
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
//a print wait 10 seconds
Try {
Thread.sleep (10000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
}
Phenomenon of two results:
CodePudding user response:
Say your own views, not necessarily to1. Sleep inside the synchronization method, sleep should be released after the lock, but then immediately got the lock again
2. Remove the sleep in the run method, also is the same thread got a lock, the reason should be like a little
3. The state of the print is not accurate
4. If you want to fair access to all threads lock, want to sleep for a little time at least
The interpretation of the thread.sleep method
Make the current thread of execution within a specified number of milliseconds to sleep (temporary stay of execution), depending on the system timer and the precision and accuracy of the scheduler, the thread does not lose ownership of any monitors,
CodePudding user response:
Thread.sleep does not release the lock, lock is released, only wait buy method use synchronized, and is a common method, so this lock is implementsRunnable object, use method, sleep is not for other threads lock, use methods, shows that the current Thread already release the lock, other threads have entered for the monitor conditions,CodePudding user response:
After sleep will release the lock, sleep time will compete to lockCodePudding user response: