1. 程式人生 > >Java 執行緒同步方式

Java 執行緒同步方式

Java中synchronized關鍵字鎖定的是物件。驗證程式碼如下:

class Demo
{
    public synchronized void  sayHello(){
        try{
            System.out.println("hello start");
            Thread.sleep(100);
            System.out.println("hello end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    public synchronized void doAction()
    {
        try{
            System.out.println("put up hand");
            Thread.sleep(100);
            System.out.println("put down hand");
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

public class Test {
    public static void main(String[] args)
    {
        Demo demo = new Demo();
        Demo demoA = new Demo();
        Thread sayThread = new Thread(new Runnable() {
            @Override
            public void run() {
                demo.sayHello();
            }
        });

        Thread actionThread = new Thread(new Runnable() {
            @Override
            public void run() {
//                demo.doAction();
                demoA.doAction();
            }
        });

        sayThread.start();
        actionThread.start();
    }
}

三種情況:

1、兩個執行緒都使用demo變數,方法用synchronized修飾。 結果: sayHello和doAction一定是一個執行完另一個才執行。

2,兩個執行緒都使用demo變數,方法不使用synchronized修飾。結果:sayHello和doAction可以交叉執行。

3、兩個執行緒分別使用demo和demoA變數,方法用synchronized修飾。 結果: sayHello和doAction可以交叉執行。

 

Semaphore 用法:

import java.util.concurrent.Semaphore;

class PreTask implements Runnable
{
    private Semaphore sem;
    private int weight;
    PreTask(Semaphore sem, int weight){
        this.sem = sem;
        this.weight = weight;
    }
    public void run()
    {
        try{
            System.out.println("a pretask starts");
            Thread.sleep(100);
            System.out.println("a pretask is done which weight is "+this.weight);
            this.sem.release(this.weight);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

class LateTask implements Runnable
{
    private Semaphore sem;
    private int requireWeight;
    LateTask(Semaphore sem, int requireWeight)
    {
        this.sem = sem;
        this.requireWeight = requireWeight;
    }
    public void run()
    {
        try{
            this.sem.acquire(this.requireWeight);
            System.out.println("a latetask starts");
            Thread.sleep(100);
            System.out.println("a latetask is done which requireWeight is "+this.requireWeight);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}
public class Test {
    public static void main(String[] args)
    {
        Semaphore semaphore = new Semaphore(0);
        PreTask preA = new PreTask(semaphore, 1);
        PreTask preB = new PreTask(semaphore, 3);
        LateTask late = new LateTask(semaphore, 4);

        Thread preThreadA = new Thread(preA);
        Thread preThreadB = new Thread(preB);
        Thread lateThreadB = new Thread(late);

        preThreadA.start();
        preThreadB.start();
        lateThreadB.start();
    }
}

 輸出結果如下:

a pretask starts
a pretask starts
a pretask is done which weight is 3
a pretask is done which weight is 1
a latetask starts
a latetask is done which requireWeight is 4

import java.util.concurrent.Semaphore;

class User implements Runnable
{
    private Semaphore sem;
    private String name;
    User(Semaphore sem, String name){
        this.sem = sem;
        this.name = name;
    }
    public void run()
    {
        try{
            this.sem.acquire(1);
            System.out.println(this.name+" take up a pen");
            Thread.sleep(100);
            System.out.println(this.name+" put down a pen ");
            this.sem.release(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}


public class Test {
    public static void main(String[] args)
    {
        Semaphore penSet = new Semaphore(2);

        User zhangsan = new User(penSet, "zhangsan");
        User lisi = new User(penSet, "lisi");
        User wangwu = new User(penSet, "wangwu");
        User zhaoliu = new User(penSet, "zhaoliu");

        Thread threadZhangsan = new Thread(zhangsan);
        Thread threadL = new Thread(lisi);
        Thread threadW = new Thread(wangwu);
        Thread threadZhaoliu = new Thread(zhaoliu);

        threadZhangsan.start();
        threadL.start();
        threadW.start();
        threadZhaoliu.start();
    }
}

 結果:最多同時有兩個人在使用鉛筆