Internet concurrent Programming (2)--Thread Basics _ Thread

Source: Internet
Author: User

Content:
Multiple locks for multiple threads, object locks and class locks
Synchronous and asynchronous object locks and class locks for object locks

Concept
More than 1 threads with multiple locks
Multiple threads, each thread can get its own specified lock, after acquiring the lock, executes the contents of the Synchronized method body.

Package com.wuk.thread;

    public class ThreadTest02 extends thread{private int num = 0;
                Public synchronized void Printnum (String tag) {try {if (Tag.equals ("a")) {num=100;
                System.out.println ("tag A, set num over!");
            Thread.Sleep (1000);
                }else {num=200;
            System.out.println ("tag b,set num over!");
        } catch (Interruptedexception e) {e.printstacktrace ();
    } System.out.println ("tag" +tag+ ", num=" +num);
        public static void Main (string[] args) {//definition create two different objects final ThreadTest02 tt1=new ThreadTest02 ();

        Final ThreadTest02 tt2=new ThreadTest02 (); Thread T1=new Thread (new Runnable () {@Override public void run () {Tt1.printnum

            ("a");

        }
        }); Thread T2=new Thread (new Runnable () {@Override Public void Run () {tt2.printnum ("B");

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

Output results:
The respective threads output their own mutual influence, because they are different objects, each holding the lock of their respective objects, so they perform non-interference.

Tag A, set num over!
Tag B,set num over!
tagb,num=200
taga,num=100

But if you add static to Printnum () so that no matter how many objects you create, the lock is only one, and it belongs to the class-level lock, not the object. That is, if you add synchronized to a static method, it means that the lock is a class-level lock, not an object lock.
Output results:
Execute each, thread 1 after the execution of the release lock, thread 2 can not be executed.

Tag A, set num over!
TAGA,NUM=100
tag b,set num over!
tagb,num=200
Synchronization and Asynchrony of object locks

Sync synchronized
The concept of synchronization is shared, and there is no need to synchronize if it is not shared.
Asynchronous asynchronized
The concept of asynchrony is independence, with no restriction on each other.

The purpose of synchronization is to be thread-safe and to meet two characteristics for thread security: atomicity (synchronization), visibility.

 package com.wuk.thread; public class ThreadTest03 extends thread{public synchronized void Method1 () {System.out.println (thread.cu
         Rrentthread (). GetName ());
        try {thread.sleep (4000);
        catch (Interruptedexception e) {e.printstacktrace ();
     }} public void Method2 () {System.out.println (Thread.CurrentThread (). GetName ());

         public static void Main (string[] args) {final ThreadTest03 tt=new ThreadTest03 (); Thread Thread01=new Thread (new Runnable () {@Override public void run () {Tt.meth
            Od1 ();

         }, "Thread01");
                    Thread Thread02=new Thread (new Runnable () {@Override public void run () {
                TT.METHOD2 ();

         }, "thread02");
         Thread01.start ();
    Thread02.start (); }
}

Results:
Print at the same time, because this is asynchronous and does not affect each other.

THREAD01
thread02

To Method1 (), METHOD2 () plus synchronized, so that two methods are synchronized, which means that thread 2 can execute only if thread 1 executes method1 () and releases the lock.

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.