多线程综合案例

多线程综合案例

Case 1:

package practicePackage;
// 设计 4 个线程对象,两个线程执行加操作,两个线程执行减操作
class Resourse {
    private int num; // 要进行加减操作的数据.
    public boolean flag = true; // 表示加和减的切换
    // flag = true;表示执行加法操作,减法操作等待
    // flag = false;表示进行减法操作,加法操作等待

    public synchronized void add() throws Exception{  // 执行加法操作
        if (this.flag == false)
        {
            super.wait();
        }
        Thread.sleep(100);
        this.num++;
        try{
            System.out.println("加法操作 - " + Thread.currentThread().getName() + " " + this.num);
        }finally{
            this.flag = false;
            super.notifyAll();
        }
    }
    public synchronized void sub() throws Exception {
        if (this.flag == true)
        {
            super.wait();
        }
        Thread.sleep(100);
        this.num--;

        try{
            System.out.println("减法操作 - " + Thread.currentThread().getName() + " " + this.num);
        }finally {
            this.flag = true;
            super.notifyAll();
        }
    }
}
class Add implements Runnable{
    private Resourse res;
    public Add(Resourse res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i=0;i<50;i++){
            try {
                this.res.add();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
class Sub implements Runnable{
    private Resourse res;
    public Sub(Resourse res) {
        this.res = res;
    }

    @Override
    public void run() {
        for (int i=0;i<50;i++){
            try {
                this.res.sub();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
public class subAdd {
    public static void main(String[] args) {
        Resourse res = new Resourse();
        Add add = new Add(res);
        Sub sub = new Sub(res);
        new Thread(add).start();
        new Thread(add).start();
        new Thread(sub).start();
        new Thread(sub).start();

    }
}

部分运行结果:

加法操作 - Thread-0 1
减法操作 - Thread-2 0
加法操作 - Thread-0 1
减法操作 - Thread-2 0

Process finished with exit code 0

Case 2:

package practicePackage;

import java.lang.management.MonitorInfo;

/*
* 设计一个生产电脑和搬运电脑类
* 要求生产出一台电脑就搬走一台电脑
* 如果没有新的电脑生产出来,则搬运工要等待新电脑生产出
* 如果生产出的电脑没有搬走,则要等待电脑搬走后再生产
* 统计出生产的电脑数量*/
class MakingComputer implements Runnable
{
    private WareHouse wh;
    public MakingComputer(WareHouse wh) {
        this.wh = wh;
    }

    @Override
    public void run() {
        for (int i=0;i<30;i++){
            try {
                this.wh.makingComputer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class MoveComputer implements Runnable
{
    private WareHouse wh;
    public MoveComputer(WareHouse wh) {
        this.wh = wh;
    }

    @Override
    public void run() {
        for (int i=0;i<30;i++){
            try {
                this.wh.moveComputer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class WareHouse{
    private int num = 0;
    private String cpu;
    private String memory;
    private boolean flag = true; // 表示生产或者搬走两种状态
    /*
    * flag = true 生产电脑,搬运工等待
    * flag = false 停止生产电脑,搬运工搬运*/

    public synchronized void makingComputer() throws InterruptedException {
        if (this.flag == false){
            super.wait();
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println( Thread.currentThread().getName() +  " 生产电脑 - 第 " + ++num + " 台");
        this.flag = false;
        super.notifyAll();
    }
    public synchronized void moveComputer() throws InterruptedException {
        if (this.flag == true){
            super.wait();
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println( Thread.currentThread().getName() +  " 搬走电脑 - 第 " + num + " 台");
        this.flag = true;
        super.notifyAll();
    }
}
public class testThread {
    public static void main(String[] args) {
        WareHouse wh = new WareHouse();
        new Thread(new MakingComputer(wh)).start();
        new Thread(new MoveComputer(wh)).start();
    }
}

部分运行结果:

Thread-1 搬走电脑 -28 台
Thread-0 生产电脑 -29 台
Thread-1 搬走电脑 -29 台
Thread-0 生产电脑 -30 台
Thread-1 搬走电脑 -30 台

Process finished with exit code 0

Case 3

package practicePackage;

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

/*
* 实现一个竞争抢答程序:
* 要求设置三个抢答者(三个线程),同时发出抢答指令
* 抢答成功者给出提示,抢答失败者给出失败提示*/
class myyThread implements Callable<String>{
    private boolean flag = false;
    @Override
    public String call() throws Exception {
        synchronized (this){
            if (this.flag == false){
                this.flag = true;
                return Thread.currentThread().getName() + " 抢答成功!";
            }else {
                return Thread.currentThread().getName() + " 抢答失败!";
            }
        }
    }
}

public class answerThread {
    public static void main(String[] args) throws Exception {
        myyThread mt = new myyThread();
        FutureTask<String> taskA = new FutureTask<String>(mt);
        FutureTask<String> taskB = new FutureTask<String>(mt);
        FutureTask<String> taskC = new FutureTask<String>(mt);
        new Thread(taskA,"抢答者A").start();
        new Thread(taskB,"抢答者B").start();
        new Thread(taskC,"抢答者C").start();
        System.out.println(taskA.get());
        System.out.println(taskB.get());
        System.out.println(taskC.get());
    }
}

运行结果:

抢答者A 抢答成功!
抢答者B 抢答失败!
抢答者C 抢答失败!

Process finished with exit code 0

你可能感兴趣的:(java)