In Java Multi-threading, how to determine whether a given class is thread-safe (in addition: Synchronized synchronization is guaranteed to ensure that the class is thread-safe.) )

Source: Internet
Author: User

The difference between synchronous code block and synchronous method: Synchronous code block can pass in arbitrary object, synchronous method if more than one thread examines a new object, different synchronization locks are not exclusive to different threads, cannot realize thread synchronization effect, then thread synchronization fails.

The difference between the two is mainly reflected in the synchronization lock above. For the synchronization method of the instance, because only this is used as a synchronous lock, if a class needs to use more than one lock, in order to avoid the collision of the lock, it is necessary to use different objects, the synchronization method can not meet the requirements, only use synchronous code block (synchronous code block can be passed to arbitrary objects);

There are several principles of:
Program Order rules: Within a thread, the code executes in the order in which the program is written
Tube Lock principle: The unlock operation of the same variable takes place in the later lock operation
Volatile variable rule: a volatile write operation occurs in the subsequent read operation
Thread Initiation Principle: Start () of thread starts all actions in the thread
Thread termination principle: All actions within a thread take precedence over thread termination detection
Thread break principle: Call Interrupt () on a thread to see if there is an interrupt occurring before the interrupted code detects
Object Termination principle: Initialization of an object occurs first in the Finalize () method
Transitivity: A first occurs in the B,b first occurs in C, then a first occurs in C

Java Multithreaded Programming Summary

http://lavasoft.blog.51cto.com/62575/27069
/**
* Java Threads: Concurrent collaboration-deadlock
*
* @author Administrator 2009-11-4 22:06:13
*/
PublicclassTest {
PublicStaticvoidMain (string[] args) {
Deadlockrisk Dead =NewDeadlockrisk ();
MyThread T1 =NewMyThread (Dead, 1, 2);
MyThread t2 =NewMyThread (Dead, 3, 4);
MyThread t3 =NewMyThread (Dead, 5, 6);
MyThread T4 =NewMyThread (Dead, 7, 8);

T1.start ();
T2.start ();
T3.start ();
T4.start ();
}

}

classMyThreadextendsThread {
PrivateDeadlockrisk dead;
PrivateintA, B;


MyThread (Deadlockrisk dead,intAintb) {
This. Dead = dead;
This. A = A;
This. B = b;
}

@Override
PublicvoidRun () {
Dead.read ();
Dead.write (A, b);
}
}

classDeadlockrisk {
PrivateStaticclassResource {
PublicintValue
}

PrivateResource Resourcea =NewResource ();
PrivateResource RESOURCEB =NewResource ();

Public intRead () {
synchronized(Resourcea) {
System.out.println ("read ():"+ Thread.CurrentThread (). GetName () +"Get the lock of Resourcea!" ");
synchronized(RESOURCEB) {
System.out.println ("read ():"+ Thread.CurrentThread (). GetName () +"Get the lock of RESOURCEB!" ");
returnResourceb.value + resourcea.value;
}
}
}

PublicvoidWriteintAintb) {
synchronized(RESOURCEB) {
System.out.println ("Write ():"+ Thread.CurrentThread (). GetName () +"Get the lock of Resourcea!" ");
synchronized(Resourcea) {
System.out.println ("Write ():"+ Thread.CurrentThread (). GetName () +"Get the lock of RESOURCEB!" ");
Resourcea.value = A;
Resourceb.value = b;
}
}
}
}

In Java Multi-threading, how to determine whether a given class is thread-safe (in addition: Synchronized synchronization is guaranteed to ensure that the class is thread-safe.) )

Related Article

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.