JAVA學習筆記(四十三)- ThreadLocal

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.