File name: Deadthreadbyextend.java
Note:
1, starting a thread with the Start method, the Run method can also be called, but only the equivalent of a normal call, executed within the current thread.
2, synchronized can not directly modify variables.
3. Synchronized block does not force single-threaded access to variables within the block. Simply means that the parameters of synchronized (args) are locked when executing the in-block statement until the execution is complete before being released.
Package com.ycf.study.thread;Classsources{int A;PublicvoidSetA(int x) {Synchronized (This) {THIS.A = x;try {thread.sleep (2000); }catch (Interruptedexception e) {e.printstacktrace ();}} }}PublicClassDeadthreadbyextend {PublicStaticvoidMain(string[] args) {Sources S1 =New Sources (); Sources s2 =New Sources ();ClassMyThread1ExtendsJava.Lang.Thread {@OverridePublicvoidRun() {System.out.println ("Thread 1 start");Synchronized (S1) {System.out.println ("Thread 1 Request modification S1"); S1.seta (20); System.out.println ("Thread 1 modification completed"); System.out.println ("Thread 1 Request modification S2"); S2.seta (10); System.out.println ("Thread 1 modification s2 completed"); } System.out.println ("Thread 1 exits and releases the lock ++++++++++"); } }ClassMyThread2ExtendsJava.Lang.thread { @Override public void run () { System.out.println ( "Thread 2 start"); synchronized (S2) {System.out.println ( "Thread 2 request modification S2"); S2.seta ( 20); System.out.println ( "Thread 2 Modify s2 complete"); System.out.println ( "Thread 2 request modification S1"); S1.seta (10); System.out.println ( "Thread 2 modify S1 complete");} System.out.println ( "Thread 2 exit and Release lock ++++++++++");}} MyThread1 MT1 = new MyThread1 (); MyThread2 mt2 = new MyThread2 (); Mt1.start (); Mt2.start ();}}
/span> Java Learning Group 669823128
Java thread deadlock