38. Wire Cheng--Multi-threaded data security and synchronous threading methods

Source: Internet
Author: User

Learning Essentials

    1. Multithreaded Data security

    2. Methods for synchronizing threads

Class MyThread implements Runnable{//mythread implementation Runnable interface int i = 100;public void run () {//Replication Run method while (true) { Synchronized (this) {//Thread synchronization code//thread.currentthread () Gets the current code on which thread is running System.out.println (Thread.CurrentThread (). GetName () + i); i--; Thread.yield ();//Let the CPU re-compete for CPU usage if (i<0) {break;}}}}
Interface test{public static void Main (String args[]) {//Generate a MyThread object myThread myThread = new MyThread ();//Generate two Thread objects , share the same thread body thread t1 = new Thread (myThread); Thread t2 = new Thread (myThread),//Each thread has a name, it can be set by the SetName method of the thread object, or it can get the thread name T1.setname with GetName ("Thread A"); T2.setname ("thread B"); T1.start (); T2.start ();}}
    1. Deep synchronized Keywords

Once a thread obtains a sync lock, all other synchronization code for that object waits for the synchronization lock to be released before it can be executed.


38. Wire Cheng--Multi-threaded data security and synchronous threading methods

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.