原创

实现Runnalbe,使用synchronized和wait和notifyAll的方式实现线程通信(生产者消费者模型),和使用Lock以及Condition的await和signalAll的方式实现线程通信()

使用Lock的方式实现线程通信

这里使用Lock的方式的时候,this.wait()和this.notifyAll()方法将无效,需要采用Condition的方式来实现
示例代码:

package com.example.demo.juc.lock;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadCommunicationLock {

    public static void main(String[] args) {
        Clerk clerk = new Clerk();

//        new Thread(new Productor(clerk)).start();
//        new Thread(new Seller(clerk)).start();

        Productor productor = new Productor(clerk);
        FutureTask futureTask = new FutureTask(productor);

        Seller seller = new Seller(clerk);
        FutureTask futureTask1 = new FutureTask(seller);

        new Thread(futureTask).start();
        new Thread(futureTask1).start();
    }
}

class Clerk{
    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    private int product = 0;
    //职员生产
    public void get(){
        try{
            lock.lock();
            while (product >= 1){
                try {
                    condition.await();
                    System.out.println(Thread.currentThread().getName() + "线程等待");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "生产产品,目前数量:" + ++product);
            condition.signalAll();
        }finally {
            lock.unlock();
        }

    }
    //职员销售功能
    public void sell(){
        try {
            lock.lock();
            while (product <= 0){
                try {
                    condition.await();
                    System.out.println(Thread.currentThread().getName() + "线程等待");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "销售产品,目前数量:" + --product);
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}

//生产过程
//class Productor implements Runnable {
class Productor implements Callable {

    private Clerk clerk;
    public Productor(Clerk clerk){
        this.clerk = clerk;
    }

//    @Override
//    public void run()   {
//        for (int i = 0; i < 10; i++) {
//            clerk.get();
//        }
//    }

    @Override
    public Object call() throws Exception  {
        for (int i = 0; i < 10; i++) {
            clerk.get();
        }
        return null;
    }
}

//销售过程
//class Seller implements Runnable{
class Seller implements Callable{

    private Clerk clerk;
    public Seller(Clerk clerk){
        this.clerk = clerk;
    }

//    @Override
//    public void run()   {
//        for (int i = 0; i < 10; i++) {
//            clerk.sell();
//        }
//
//    }

    @Override
    public Object call() throws Exception  {
        for (int i = 0; i < 10; i++) {
            clerk.sell();
        }
        return null;
    }
}

使用synchronized的方式实现线程通信

示例代码中采用了实现Runnable(代码已经注释)

示例代码:

package com.example.demo.juc.synchronize;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class ThreadCommunicationSynchronized {

    public static void main(String[] args) {
        Clerk clerk = new Clerk();

//        new Thread(new Productor(clerk)).start();
//        new Thread(new Seller(clerk)).start();

        Productor productor = new Productor(clerk);
        FutureTask futureTask = new FutureTask(productor);

        Seller seller = new Seller(clerk);
        FutureTask futureTask1 = new FutureTask(seller);

        new Thread(futureTask).start();
        new Thread(futureTask1).start();
    }
}

class Clerk{
    private int product = 0;
    //职员生产
    public synchronized void get(){
        while (product >= 1){
            try {
                this.wait();
                System.out.println(Thread.currentThread().getName() + "线程等待");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "生产产品,目前数量:" + ++product);
        this.notifyAll();
    }
    //职员销售功能
    public synchronized void sell(){
        while (product <= 0){
            try {
                this.wait();
                System.out.println(Thread.currentThread().getName() + "线程等待");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "销售产品,目前数量:" + --product);
        this.notifyAll();
    }
}

//生产过程
//class Productor implements Runnable {
class Productor implements Callable {

    private Clerk clerk;
    public Productor(Clerk clerk){
        this.clerk = clerk;
    }

//    @Override
//    public void run()   {
//        for (int i = 0; i < 10; i++) {
//            clerk.get();
//        }
//    }

    @Override
    public Object call() throws Exception  {
        for (int i = 0; i < 10; i++) {
            clerk.get();
        }
        return null;
    }
}
//销售过程
//class Seller implements Runnable{
class Seller implements Callable{

    private Clerk clerk;
    public Seller(Clerk clerk){
        this.clerk = clerk;
    }

//    @Override
//    public void run()   {
//        for (int i = 0; i < 10; i++) {
//            clerk.sell();
//        }
//
//    }

    @Override
    public Object call() throws Exception  {
        for (int i = 0; i < 10; i++) {
            clerk.sell();
        }
        return null;
    }
}
正文到此结束
本文目录