Java notes 14__ Multithreading shared data (synchronous)/thread deadlock/

Source: Internet
Author: User

/*** Multithreading shared data * thread synchronization: Multiple threads can have only one thread executing their specified code at the same time period, and other threads will wait until the thread finishes before continuing. * Multi-threaded sharing of data security issues, using synchronous resolution. * Thread synchronization two methods: * 1. Synchronizing code blocks * Synchronized (objects to synchronize) {action to synchronize} * 2. Synchronization method * Public synchronized void method () {Action to synchronize} 
    */ Public classMain { Public Static voidMain (string[] args) {MyThread S0=NewMyThread (); Thread T1=NewThread (S0, "one"); Thread T2=NewThread (S0, "both"));        T1.start ();    T2.start (); }}classMyThreadImplementsrunnable{Object obj=NewObject ();//tagged objects that are synchronized@Override Public voidrun () {//Synchronizing code blocks        synchronized(obj) {System.out.println (Thread.CurrentThread (). GetName ()+ "is doing ..."); Try{Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ "finished."); }    }}

 Public classMain { Public Static voidMain (string[] args) {MyThread S0=NewMyThread (); Thread T1=NewThread (S0, "one"); Thread T2=NewThread (S0, "both"));        T1.start ();    T2.start (); }}classMyThreadImplementsrunnable{@Override Public voidrun () {Domethod (); }    /*** Synchronization method, synchronization is the current object (this)*/     Public synchronized voidDomethod () {System.out.println (Thread.CurrentThread (). GetName ()+ "is doing ..."); Try{Thread.Sleep (1000); } Catch(Interruptedexception ex) {ex.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName ()+ "finished."); }}/*** Synchronization of code can lead to performance degradation issues, high data security * when writing synchronized blocks, there are several simple guidelines to follow. * 1. Keep the code block short and move out of the synchronized block with preprocessing and post-processing that do not change thread. * 2. Do not block. such as Inputstream.read (). * 3. Do not invoke methods on other objects while holding the lock. */

/*** Thread deadlock: Too many synchronizations can cause deadlocks*/ Public classMain { Public Static voidMain (string[] args) {NewMyThread (); }}classMyThreadImplementsrunnable{Customer C=NewCustomer (); Waiter W=NewWaiter ();  PublicMyThread () {NewThread ( This). Start ();    W.say (c); } @Override Public voidrun () {C.say (w); }}classcustomer{//Customer     Public synchronized voidSay (Waiter W) {System.out.println (The customer said: Do it first and then give it to the money! ");    W.doservice (); }     Public synchronized voidDoservice () {System.out.println ("The customer agreed, first give money to do"); }}classwaiter{//Waiter     Public synchronized voidsay (Customer c) {System.out.println (The waiter said: "First give money to do again!" ");    C.doservice (); }     Public synchronized voidDoservice () {System.out.println ("The waiter agreed, do the money first."); }}
The Java language keyword that, when used to decorate a method or a block of code, guarantees that at most one thread executes the code at the same time.
1. When two concurrent threads access the synchronized (this) synchronization code block in the same object, only one thread can be executed within a single time. The other thread must wait for the current thread to finish executing the block before it can execute the code block.
2. However, when one thread accesses one synchronized (this) of an object to synchronize a block of code, another thread can still access the non-synchronized (this) synchronous code block in that object.
3. In particular, when a thread accesses one of the synchronized (this) synchronization blocks of object, other threads will block access to all other synchronized (this) synchronous blocks of code in object.
4. The third example also applies to other synchronization code blocks. That is, when a thread accesses a synchronized (this) of object to synchronize a block of code, it obtains the object lock of the objects. As a result, other threads are temporarily blocking access to all of the synchronization code portions of the object.
5. The above rules also apply to other object locks.


Java notes 14__ Multithreading shared data (synchronous)/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.