Java thread synchronization - Thread.sleep () method Does not work as desired

I heard sleep () will block the current method / sync block. But here, when I call sleep () on thread 1, thread 2 can access the same block? Can anyone explain?

Main.java

public class Main { public static void main(String args[]) { Thread1 t1 = new Thread1(); Thread2 t2 = new Thread2(); System.out.println("going to start t1"); t1.start(); System.out.println("going to start t2"); t2.start(); } } 

==================================================== =====================

Thread1.java

 public class Thread1 extends Thread{ public void run() { Syncc s1 = new Syncc(); s1.me("T1:"); } } 

==================================================== =====================

Thread2.java

 public class Thread2 extends Thread{ public void run() { Syncc s2 = new Syncc(); s2.me("T2:"); } } 

==================================================== =====================

Syncc.java

 public class Syncc{ public void me(String s){ synchronized(this){ for(int i=0; i<=5; i++) { System.out.println(s+" "+" "+i); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } } } 

============================================

Conclusion:

 going to start t1 going to start t2 T2: 0 T1: 0 T2: 1 T1: 1 T1: 2 T2: 2 T1: 3 T2: 3 T1: 4 T2: 4 T2: 5 T1: 5 

BUT according to the sleep () method, it should not immediately block the current synchronization block? if so, then there should be a way out ..

starts t1 starts t2

 T1: 0 T1: 1 T1: 2 T1: 3 T1: 4 T1: 5 T2: 0 T2: 1 T2: 2 T2: 3 T2: 4 T2: 5 

i means that after thread 1 is executed, only thread 2 should start right? What is the problem?

+4
source share
3 answers

This is due to the fact that the game has two different instances of Syncc . Each stream has its own copy of Syncc .

Try to do the same with a single instance. You can also synchronize the static context and try.

To simulate, modify Thread1 and Thread2 to accept an instance of Syncc .

 public class Thread1 extends Thread { private Syncc syncc; public Thread1(Syncc syncc) { this.syncc = syncc; } public void run() { this.syncc.me("T1:"); } } 

Then you can run them like this:

 public static void main(String args[]) { Syncc syncc = new Syncc(); Thread1 t1 = new Thread1(syncc); Thread2 t2 = new Thread2(syncc); System.out.println("going to start t1"); t1.start(); System.out.println("going to start t2"); t2.start(); } 
+9
source

Rules for sleep, exit and connection

  • Sleep mode is used to delay execution for a certain period of time, and no locks are released when the thread goes into sleep mode.

  • The sleeping thread will be guaranteed to sleep at least for the time specified in the argument to the sleep () method (if it is not interrupted) , but there is no guarantee as to when the newly awakened thread will return to work.

  • The sleep () method is a static method that executes a hibernation state of a thread. One thread cannot tell another thread to sleep.

  • The setPriority () method is used for Thread objects to provide threads with priority between 1 (low) and 10 (high), although priorities are not guaranteed, and not all JVMs recognize 10 different priority levels - some levels can be considered effective equal.

  • Unless explicitly specified, the priority of the thread will have the same priority as the priority of the thread that created it.

  • The yield () method can cause a working thread to backtrack if there are current threads with the same priority. There is no guarantee that this will happen, and there is no guarantee that when the thread returns from there, another thread selected to run will be selected. The thread may give way, and then immediately start the operating state.

  • The immediate guarantee is that at any point in time, when a thread, it usually does not have a lower priority than any thread in a managed state. If a low-priority thread is running when a high-priority thread enters runnable, the JVM tends to crowd out the low-priority job and deliver a high-priority thread.

  • When one thread calls the join () method of another thread, the current running thread will wait for the thread to which it joins to end. To think of the join () method, saying: "Hello, thread, I want to join the end of you. Let me know when you are done, so I can enter the runnable state."

http://www.amazon.com/SCJP-Certified-Programmer-Java-310-065/dp/0071591060

+2
source

You have created two Synch objects for each object corresponding to one thread. Each object has its own copy of me. Therefore, when you start each thread using the run method, the thread calls its own copy of the me function. Since two threads only act on their copy, this works as a single thread scenario. If you want to test a multithreaded script, make the method static (class-level method) and apply class-level locking.

Thread1.java

 public class Thread1 extends Thread{ public void run() { Syncc.me("T1:"); } 

}

Thread2.java

 public class Thread2 extends Thread{ public void run() { Syncc.me("T2:"); } 

}

Syncc.java

 public class Syncc{ public static void me(String s){ synchronized(Syncc.class){ for(int i=0; i<=5; i++) { System.out.println(s+" "+" "+i); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } } 

}}

0
source

All Articles