36-多线程综合案例

多线程综合实例

  

数字加减

【题目】设计四个线程对象,两个线程执行减操作,两个线程执行加操作。
  

class Resource{
    
    		//操作资源
	private int num = 0;		//进行加减操作的数据
	private boolean flag = true;		//加减切换,
	//flag=true 表示可以进行加法,不能进行减法
	public synchronized void add() throws Exception {
    
    
		while(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 Exception {
    
    
		while(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;
	}
	@Override
	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;
	}
	@Override
	public void run() {
    
    
		for(int x =0; x<50; x++) {
    
    
			try {
    
    
				this.resource.sub();
			} catch (Exception e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

public class Num {
    
    
	public static void main(String[] args) {
    
    
		Resource res = new Resource();
		AddThread at = new AddThread(res);
		SubThread su = new SubThread(res);
		new Thread(at,"加法线程 - A").start();
		new Thread(at,"加法线程 - B").start();
		new Thread(su,"减法线程 - X").start();
		new Thread(su,"减法线程 - Y").start();
	}
}

本题是一个经典的多线程开发操作,程序中要考虑的核心本质是:加一个、减一个,整体的计算结果应该只在0、1、-1之间循环出现。

生产电脑

【题目】设计一个生产电脑和搬运电脑类,要求生产出一台电脑就搬走一台电脑,如果没有新的电脑生产,则搬运工要等待;如果新电脑没有被搬走,则生产要等待,并统计生产电脑数量。
生产者消费者模型

class Computer{
    
    
	private static int count = 0;
	private String name;
	private double price;
	public Computer(String name, double price) {
    
    
		this.name = name;
		this.price = price;
		this.count++;
	}
	@Override
	public String toString() {
    
    
		return "num = "+ count +" Computer [name=" + name + ", price=" + price + "]";
	}
}

class Resources {
    
    		//
	private Computer computer;
	public synchronized void product() throws Exception{
    
    
		if(this.computer != null) {
    
    		//已经生产过了
			super.wait();
		}
		Thread.sleep(100);
		this.computer = new Computer("lyz", 1.1);
		System.out.println("生产电脑"+ this.computer);
		super.notifyAll();
	}
	public synchronized void consume() throws Exception {
    
    
		if(this.computer == null) {
    
    		//不能进行搬运
			super.wait();
		}
		Thread.sleep(10);
		this.computer = new Computer("lyz", 1.1);
		super.notifyAll();
		System.out.println("取走电脑"+this.computer);
		this.computer = null;		//已经取走了
		super.notifyAll();
	}
}

class Produced implements Runnable{
    
    
	private Resources resource;
	public Produced(Resources res) {
    
    
		this.resource = res;
	}
	@Override
	public void run() {
    
    
		for(int x =0; x<50;x++) {
    
    
			try {
    
    
				this.resource.product();
			} catch (Exception e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

class Consum implements Runnable{
    
    
	private Resources resource;
	public Consum(Resources res) {
    
    
		this.resource = res;
	}
	@Override
	public void run() {
    
    
		for(int x =0; x<50;x++) {
    
    
			try {
    
    
				this.resource.consume();
			} catch (Exception e) {
    
    
				e.printStackTrace();
			}
		}
	}
}

public class ComputerDemo {
    
    
	public static void main(String[] args) {
    
    
		Resources res = new Resources();
		new Thread(new Produced(res)).start();
		new Thread(new Consum(res)).start();
	}
}

  

竞争抢答

【题目】实现一个竞拍抢答程序,要求设置三个抢答者(三个线程),同时发出抢答指令,抢答成功或失败要给出提示。
这样一个多线程的操作由于里面需要牵扯到数据的返回问题,那么现在最好使用的Callable是比较方便的处理形式。

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

class AnswerThread 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 AnswerDemo {
    
    
	public static void main(String[] args) throws Exception{
    
    
		AnswerThread mt  = new AnswerThread();
		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());
	}
}

猜你喜欢

转载自blog.csdn.net/MARVEL_3000/article/details/111567531
今日推荐