00 05Java高级之多线程综合案例

1 数字加减

设计4个线程对象,两个线程执行减操作,两个线程执行加操作。

package cn.victor.demo;

public class ThreadDemo {
	
	public static void main(String[] args) throws InterruptedException {
		Resource res = new Resource();
		Runnable pro = new Producer(res);
		Runnable con = new Consumer(res);
		new Thread(pro, "ADD-1").start();
		new Thread(pro, "ADD-2").start();
		new Thread(con, "SUB-A").start();
		new Thread(con, "SUB-B").start();
	}		
}

class Producer implements Runnable{
	private Resource res;
	
	public Producer() {}
	
	public Producer(Resource res) {
		this.res = res;
	}
	
	@Override
	public void run() {
		for(int i = 0; i < 50; i++) {
			try {
				this.res.addOperator();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

class Consumer implements Runnable{
	private Resource res;
	
	public Consumer() {}
	
	public Consumer(Resource res) {
		this.res = res;
	}
	
	@Override
	public void run() {
		for(int i = 0; i < 50; i++) {
			try {
				this.res.subOperator();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

class Resource{
	private int sum = 0;
	private boolean flag = true;
	
	public synchronized void addOperator() throws Exception{
		if(!this.flag) {
			super.wait();
		}
		Thread.sleep(10);
		this.sum++;
		System.out.println(Thread.currentThread().getName() + " add:" + this.sum);
		this.flag = false;
		super.notifyAll();
	}
	
	public synchronized void subOperator() throws Exception{
		if(this.flag) {
			super.wait();
		}
		Thread.sleep(10);
		this.sum--;
		System.out.println(Thread.currentThread().getName() + " sub:" + this.sum);
		this.flag = true;
		super.notifyAll();
	}
}



2 生产电脑

设计一个生产电脑和搬运电脑类,要求生产出一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑生产出;如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出生产的电脑数量。
在本程序中实现的就是一个标准的生产者与消费者的处理模型,那么下面实现具体的程序代码:

package cn.victor.demo;

public class ThreadDemo {
	
	public static void main(String[] args) throws InterruptedException {
		Resource res = new Resource();
		Producer pro = new Producer(res);
		Consumer con = new Consumer(res);
		new Thread(pro, "make").start();
		new Thread(con, "get").start();
	}		
}

class Producer implements Runnable{
	private Resource res;
	
	public Producer() {}
	
	public Producer(Resource res) {
		this.res = res;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			try {
				this.res.make();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

class Consumer implements Runnable{
	private Resource res;
	
	public Consumer() {}
	
	public Consumer(Resource res) {
		this.res = res;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			try {
				this.res.get();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

class Resource{
	private Computer computer;
	
	public Resource() {}
	
	public Resource(Computer computer) {
		this.computer = computer;
	}
	
	public synchronized void make() throws Exception{
		if(this.computer != null) {
			super.wait();
		}
		Thread.sleep(500);
		this.computer = new Computer("lks", 100);
		System.out.println("MC -- " + this.computer);
		super.notify();
	}
	
	public synchronized void get() throws Exception{
		if(this.computer == null) {
			super.wait();
		}
		System.out.println("GC -- " + this.computer);
		this.computer = null;
		super.notifyAll();
	}
}

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


3 竞争抢答

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

对于这一个多线程的操作由于里面需要牵扯到数据的返回问题,那么现在最好使用的Callable是一种比较方便的处理形式。

package cn.victor.demo;

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

class Resource implements Callable<String>{

	private boolean flag = true; 
	
	@Override
	public String call() throws Exception {
		// TODO Auto-generated method stub
		synchronized(this) {
			if(this.flag) {
				this.flag = false;
				return Thread.currentThread().getName() + " get success!";
			}
			else {
				return Thread.currentThread().getName() + " get fail!";
			}
		}
	}
	
}

public class ThreadDemo {
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		Resource res = new Resource();
		FutureTask<String> ftA = new FutureTask<String>(res);
		FutureTask<String> ftB = new FutureTask<String>(res);
		FutureTask<String> ftC = new FutureTask<String>(res);
		new Thread(ftA, "lks").start();
		new Thread(ftB, "hhy").start();
		new Thread(ftC, "zsl").start();
		System.out.println(ftA.get());
		System.out.println(ftB.get());
		System.out.println(ftC.get());
	}		
}


发布了87 篇原创文章 · 获赞 11 · 访问量 3000

猜你喜欢

转载自blog.csdn.net/weixin_43762330/article/details/104769996
00