Java thread deadlock

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.