Learning Essentials
Multithreaded Data security
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 ();}}
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