14.10 线程自我检测

1.设计4个线程对象,其中两个执行减操作,另外两个执行加操作。

package cn.kuiba.util;

class Resource{
    private int num=0;            //加减操作的数据
    private boolean flag=true;    //转换加减线程的标志
    //flag=true:加法线程执行,减法等待
    //flag=true:减法线程执行,加法等待
    public synchronized void add()throws InterruptedException{
        if (this.flag==false){
            super.wait();
        }
        Thread.sleep(100);
        this.num++;
        System.out.println("【执行加法线程-"+Thread.currentThread().getName()+"】num="+this.num);
        this.flag=false;
        super.notifyAll();
    }
    public synchronized void sub() throws InterruptedException{
        if (this.flag==true){
            super.wait();
        }
        Thread.sleep(200);
        this.num--;
        System.out.println("【执行减法线程-" + Thread.currentThread().getName() + "】num=" + this.num);
        this.flag=true;
        super.notifyAll();
    }
}
class AddThread implements Runnable{
    private Resource resource;
    public AddThread(Resource resource){
        this.resource=resource;
    }
    public void run(){
        for (int x=0;x<50;x++){
            try {
                this.resource.add();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
class SubThread implements Runnable{
    private Resource resource;
    public SubThread(Resource resource){
        this.resource=resource;
    }
    public void run(){
        for (int x=0;x<50;x++){
            try {
                this.resource.sub();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
public  class Main {
    public static void main(String[] args) throws Exception {
        Resource resource=new Resource();
        AddThread add=new AddThread(resource);
        SubThread sub=new SubThread(resource);
        new Thread(add,"加法线程A").start();
        new Thread(add,"加法线程X").start();
        new Thread(sub,"减法线程B").start();
        new Thread(sub,"减法线程Y").start();
    }
}




程序执行结果(截取部分数据):
【执行加法线程-加法线程A】num=1
【执行减法线程-减法线程Y】num=0
【执行减法线程-减法线程B】num=1
【执行加法线程-加法线程X】num=0
【执行减法线程-减法线程B】num=1
【执行减法线程-减法线程Y】num=0
【执行加法线程-加法线程A】num=1
【执行减法线程-减法线程Y】num=0
【执行减法线程-减法线程B】num=1

2.设计一个生产计算机和搬运计算机类,要求生产出一台计算机就搬走一台计算机,如果没有新的计算机生产出来,则搬运工要等待新计算机产出;如果生产出的计算机没有搬走,则要等待计算机搬走后再生产,并统计出生产计算机的数量。

package cn.kuiba.util;

class Computer{
    private static int count=0;         //表示生产的个数
    private String name;                //电脑名称
    private double prize;               //电脑价格
    public Computer(String name,double prize){
        this.name=name;
        this.prize=prize;
        count++;
    }
    @Override
    public String toString(){
        return "【第"+count+"台】电脑名字:"+this.name+"、价格:"+this.prize;
    }
}
class Resource{
    private Computer computer;
    public synchronized void make()throws Exception{
        if (this.computer!=null){
            super.wait();
        }
        Thread.sleep(100);
        this.computer=new Computer("魁拔",3333);
        System.out.println("生产电脑"+this.computer);
        super.notify();
    }
    public synchronized void get()throws Exception{
        if (this.computer==null){
            super.wait();
        }
        Thread.sleep(10);
//        System.out.println(this.computer);
        System.out.println("取走电脑"+this.computer);
        this.computer=null;
        super.notify();
    }
}
class Producer implements Runnable{
    private Resource resource;
    public Producer(Resource resource){
        this.resource=resource;
    }
    public void run(){
        for (int x=0;x<10;x++){
            try {
                this.resource.make();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
class Consumer implements Runnable{
    private Resource resource;
    public Consumer(Resource resource){
        this.resource=resource;
    }
    public void run(){
        for (int x=0;x<10;x++){
            try {
                this.resource.get();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
public  class Main {
    public static void main(String[] args) throws Exception {
        Resource res=new Resource();
        /*new Thread(new Producer(res)).start();
        new Thread(new Consumer(res)).start();*/
        Producer pc=new Producer(res);
        Consumer co=new Consumer(res);
        new Thread(pc).start();
        new Thread(co).start();
    }
}




程序执行结果:
生产电脑【第1台】电脑名字:魁拔、价格:3333.0
取走电脑【第1台】电脑名字:魁拔、价格:3333.0
生产电脑【第2台】电脑名字:魁拔、价格:3333.0
取走电脑【第2台】电脑名字:魁拔、价格:3333.0
生产电脑【第3台】电脑名字:魁拔、价格:3333.0
取走电脑【第3台】电脑名字:魁拔、价格:3333.0
生产电脑【第4台】电脑名字:魁拔、价格:3333.0
取走电脑【第4台】电脑名字:魁拔、价格:3333.0
生产电脑【第5台】电脑名字:魁拔、价格:3333.0
取走电脑【第5台】电脑名字:魁拔、价格:3333.0
生产电脑【第6台】电脑名字:魁拔、价格:3333.0
取走电脑【第6台】电脑名字:魁拔、价格:3333.0
生产电脑【第7台】电脑名字:魁拔、价格:3333.0
取走电脑【第7台】电脑名字:魁拔、价格:3333.0
生产电脑【第8台】电脑名字:魁拔、价格:3333.0
取走电脑【第8台】电脑名字:魁拔、价格:3333.0
生产电脑【第9台】电脑名字:魁拔、价格:3333.0
取走电脑【第9台】电脑名字:魁拔、价格:3333.0
生产电脑【第10台】电脑名字:魁拔、价格:3333.0
取走电脑【第10台】电脑名字:魁拔、价格:3333.0

3.实现一个竞拍抢答程序;要求设置三个抢答者(三个线程),而后同时发出抢答指令,抢答成功者给出成功提示,未抢答成功者给出失败提示。

package cn.kuiba;

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

class MyThread 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 Main {
    public static void main(String[] args)throws Exception{
        MyThread mt =new MyThread();
        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抢答失败!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值