標籤:thread 死結
死結
/* * 死結:多個線程使用不同的鎖,每個線程都需要另一個線程持有的鎖 * 出現:同步的嵌套 * */public class Test01 { public static void main(String[] args) { Tom tom = new Tom(); Alice alice = new Alice(); MyThread mt1 = new MyThread(tom, alice, true); MyThread mt2 = new MyThread(tom, alice, false); Thread th1 = new Thread(mt1); Thread th2 = new Thread(mt2); th1.start(); th2.start(); }}class MyThread implements Runnable { private Tom tom; private Alice alice; boolean flag; public MyThread(Tom tom, Alice alice, boolean flag) { this.tom = tom; this.alice = alice; this.flag = flag; } @Override public void run() { if (flag) { synchronized (tom) { // tom鎖 tom.say(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (alice) { // alice鎖 tom.show(); } } } else { synchronized (alice) { // alice鎖 alice.say(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (tom) {// tom鎖 alice.show(); } } } }}/* * tom需要筆 */class Tom { public void say() { System.out.println("給我筆"); } public void show() { System.out.println("得到筆"); }}/* * alice需要筆 */class Alice { public void say() { System.out.println("給我本子"); } public void show() { System.out.println("得到本子"); }}
ThreadLocal
import java.util.Random;/* * ThreadLocal * 為每個線程提供一個變數的副本,不同的線程擷取的變數不同 * 線程局部變數的含義,即產生的變數為某個線程所有,對此線程是局部的 */public class Test03 { public static void main(String[] args) { MyThread3 mt=new MyThread3(); Thread th1=new Thread(mt, "線程一"); Thread th2=new Thread(mt, "線程二"); Thread th3=new Thread(mt, "線程三"); Thread th4=new Thread(mt, "線程四"); th1.start(); th2.start(); th3.start(); th4.start(); }}class MyThread3 implements Runnable { //建立一個線程局部變數,用於管理多線程要操作的對象 private static ThreadLocal<Student> local=new ThreadLocal<Student>(); //擷取線程局部變數的方法 public Student getStudent(){ //第一次擷取時,擷取的值必須為空白 Student stu = local.get(); if(stu==null){ stu=new Student();//第一次擷取對象時沒有學生,所以需要手動建立一個對象 local.set(stu);//將學生對象交由ThreadLocal管理 } return stu; } @Override public void run() { Random r = new Random(); int age = r.nextInt(100); //從ThreadLocal中擷取本線程的學生對象 Student stu=getStudent(); stu.setAge(age); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "***" + stu.getAge()); }}
synchronized和ThreadLocal區別
import java.util.Random;/* * synchronized和ThreadLocal區別 * synchronized用於線程間的資料共用,而ThreadLocal則用於線程間的資料隔離 */public class Test02 { public static void main(String[] args) { Student stu=new Student(); MyThread2 mt=new MyThread2(stu); Thread th1=new Thread(mt, "線程一"); Thread th2=new Thread(mt, "線程二"); th1.start(); th2.start(); }}class MyThread2 implements Runnable { private Student stu; public MyThread2(Student stu) { this.stu = stu; } @Override public void run() { while(true){ Random r = new Random(); int age = r.nextInt(100); synchronized (stu) { stu.setAge(age); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "***" + stu.getAge()); } } }}/* * 學生類 */class Student { private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
JAVA學習筆記(四十三)- ThreadLocal