Dead lock
/* Deadlock: Multiple threads use different locks, each thread requires a lock held by another thread * appears: synchronized nesting * */ Public class Test01 { Public Static void Main(string[] args) {Tom Tom =NewTom (); Alice Alice =NewAlice (); MyThread MT1 =NewMyThread (Tom, Alice,true); MyThread mt2 =NewMyThread (Tom, Alice,false); Thread Th1 =NewThread (MT1); Thread Th2 =NewThread (MT2); Th1.start (); Th2.start (); }}class MyThread implements Runnable {PrivateTom Tom;PrivateAlice Alice;BooleanFlag Public MyThread(Tom Tom, Alice Alice,BooleanFlag) { This. Tom = Tom; This. Alice = Alice; This. flag = Flag; }@Override Public void Run() {if(flag) {synchronized(Tom) {//Tom LockTom.say ();Try{Thread.Sleep (Ten); }Catch(Interruptedexception e) {E.printstacktrace (); }synchronized(Alice) {//Alice LockTom.show (); } } }Else{synchronized(Alice) {//Alice LockAlice.say ();Try{Thread.Sleep (Ten); }Catch(Interruptedexception e) {E.printstacktrace (); }synchronized(Tom) {//Tom LockAlice.show (); } } } }}/ * * Tom needs a pen * *Class Tom { Public void say() {System.out.println ("Give me a pen."); } Public void Show() {System.out.println ("Get the pen"); }}/ * * Alice needs a pen */Class Alice { Public void say() {System.out.println ("Give me the book."); } Public void Show() {System.out.println ("Get the book."); }}
ThreadLocal
Import Java.util.Random;/* * ThreadLocal * Provides a copy of a variable for each thread, different threads get different variables * The meaning of the thread local variables, that is, the resulting variable is a thread-owned, the thread is local * / Public class Test03 { Public Static voidMain (string[] args) {MyThread3 mt=NewMyThread3 (); Thread th1=NewThread (MT,"Thread One"); Thread th2=NewThread (MT,"Thread Two"); Thread th3=NewThread (MT,"Thread three"); Thread th4=NewThread (MT,"Thread Four"); Th1.start (); Th2.start (); Th3.start (); Th4.start (); }} class MyThread3 implements Runnable { //Create a thread local variable for managing multi-threaded objects to manipulate Private StaticThreadlocal<student> local=NewThreadlocal<student> ();//method to get thread local variables PublicStudent getstudent () {//First fetch, the obtained value must be emptyStudent stu = Local.get ();if(stu==NULL) {stu=NewStudent ();//The first time you get an object without a student, so you need to create an object manuallyLocal.set (Stu);//Threadlocal The student object to the management}returnStu } @Override Public voidRun () {Random r =NewRandom ();intAge = R.nextint ( -);//Get the Student object of this thread from ThreadlocalStudent stu=getstudent (); Stu.setage (age);Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {E.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () +"***"+ stu.getage ()); }}
Synchronized and threadlocal differences
ImportJava.util.Random;/* * Synchronized and threadlocal differences * synchronized for data sharing between threads, while threadlocal is used for data isolation between threads */ Public class Test02 { Public Static void Main(string[] args) {Student stu=NewStudent (); MyThread2 mt=NewMyThread2 (Stu); Thread th1=NewThread (MT,"Thread One"); Thread th2=NewThread (MT,"Thread Two"); Th1.start (); Th2.start (); }}class MyThread2 implements Runnable {PrivateStudent Stu; Public MyThread2(Student Stu) { This. stu = Stu; }@Override Public void Run() { while(true) {Random R =NewRandom ();intAge = R.nextint ( -);synchronized(Stu) {Stu.setage (age);Try{Thread.Sleep ( +); }Catch(Interruptedexception e) {E.printstacktrace (); } System.out.println (Thread.CurrentThread (). GetName () +"***"+ stu.getage ()); } } }}*/* Student class * *Class Student {Private intAge Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }}
Java Learning Note (43)-ThreadLocal