Learn Java multithreading with examples-3

Source: Internet
Author: User

Synchronizing code blocks is an efficient way to implement atomic operations, and in the previous chapter we talked about some of the basics of synchronous atomic operations.

Now let's recall two questions from the previous chapter.

1: What is the difference between the different synchronized, and how do you write the code that creates the thread?

To use a class instance object as the locking notation

Notation 1

Package com.home.thread;/** * @author Gaoxu *  */public class Safethread {@safepublic  void Testprint () { Synchronized (Safethread.class) {System.out.println ("Enter Testprint Method!"); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Exit Testprint Method!");}}}


Notation 2

Package com.home.thread;/** * @author Gaoxu *  */public class Safethread {public static   synchronized  void Testprint () {System.out.println ("Enter Testprint Method!"); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Exit Testprint Method!");}}

The above two kinds of writing is the class instance object as a lock, how to write these two ways of calling the thread? Let's take a look at the following example

1, create the current object instance, and initialize the thread with the object instance.

Package com.home.thread;/** * @author Gaoxu *  */public class ThreadStart {public static void main (string[] para) {Safet Hread safe = new Safethread (), for (int i=0;i<3;i++) {ThreadRead1 T1 = new ThreadRead1 (safe); T1.start ();}}}

Package com.home.thread;/** * @author Gaoxu * */public class ThreadRead1 extends thread{safethread safe = null;public thre AdRead1 () {}public ThreadRead1 (Safethread o) {safe = O;} public void Run () {safe.testprint ();}}


2, you can create class instances in a thread.

Package com.home.thread;/** * @author Gaoxu *  */public class ThreadStart {public static void main (string[] para) {for (i NT i=0;i<3;i++) {ThreadRead1 T1 = new ThreadRead1 (); T1.start ();}}} </span>


<span style= "FONT-SIZE:14PX;" >package com.home.thread;/** * @author Gaoxu * */public class ThreadRead1 extends thread{safethread safe = null;public ThreadRead1 () {}public void run () {safe = new Safethread (); Safe.testprint ();}}


These two general notation can play the same role, all can realize the atomic operation, realizes the synchronous mutually exclusive invocation.


Creates an internal synchronization code block that takes the current instance object as a lock object.

Notation 1

Package com.home.thread;/** * @author Gaoxu *  */public class Safethread {public   synchronized  void Testprint () {System.out.println ("Enter Testprint Method!"); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Exit Testprint Method!");}}


Package com.home.thread;/** * @author Gaoxu *  */public class Safethread {public  void Testprint () {synchronized ( This) {System.out.println ("Enter Testprint Method!"); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Exit Testprint Method!");}}} <span style= "FONT-SIZE:14PX;" ></span>
Package com.home.thread;/** * @author Gaoxu *  */public class Safethread {Object A = new Object ();p ublic   void TESTP Rint () {synchronized (a) {System.out.println ("Enter Testprint Method!"); try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Exit Testprint Method!");}}}

These three kinds of writing are based on the class's current instance object as the lock object, so the thread invocation is written as follows;

Package com.home.thread;/** * @author Gaoxu *  */public class ThreadStart {public static void main (string[] para) {Safet Hread safe = new Safethread ();//Current instance object for (int i=0;i<3;i++) {ThreadRead1 T1 = new ThreadRead1 (safe); T1.start ();}}}
Package com.home.thread;/** * @author Gaoxu * */public class ThreadRead1 extends thread{safethread safe = null;public thre AdRead1 () {}public ThreadRead1 (Safethread o) {safe = O;} public void Run () {safe.testprint ();}}

Let's look at the difference between a class object and the current object:

Classes. class and static synchronized are the shackles of all instance objects of that class.

Synchronized (this), Synchronized,synchronized (object) is a lock on the current instance object of the class.

Specific Description:

synchronized is a lock on the current instance of a class that prevents other threads from accessing all synchronized blocks of the current instance of the class at the same time. the static synchronized is access to all instances of the control class, and static synchronized is the code that restricts the thread to access all instances of that class in the JVM at the same time. There is a synchronized in a method or block of code in a class, then after generating an instance of that class, the class has a monitoring block that is placed concurrently to access the instance synchronized Protection block, which is valid only for the current instance, and the static Synchronized is a common monitoring block for all instances of the class, and the placement thread accesses the protection block of all instances of the class concurrently, synchronized the equivalent of this.synchronized, and
The static synchronized is equivalent to something.synchronized.

2: How the deadlock, the active question all produces.

The following chapters focus on our discussion.

J today's question is:

1: Thread Safety There is nothing to note but atomic operations.

2: How to determine that you need to implement a thread-safe class.



Learn Java multithreading with examples-3

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.