java多线程编程综合案例

Java多线程综合案例

数字加减

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

public class ThreadDemo{
	public static void main(String[] args) throws Exception {
		Resource res=new Resource();
		AddThread at=new AddThread(res);
		SubThread st=new SubThread(res);
		new Thread(at,"加法线程A:").start();
		new Thread(at,"加法线程B:").start();
		new Thread(st,"减法线程X:").start();
		new Thread(st,"减法线程Y:").start();
		
		
		
	}
}
class AddThread implements Runnable{//加法操作
	private Resource resource;
	public AddThread(Resource resource) {
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int x=0;x<50;x++) {
			try {
				this.resource.add();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
class SubThread implements Runnable{//减法操作
	private Resource resource;
	public SubThread(Resource resource) {
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int x=0;x<50;x++) {
			try {
				this.resource.sub();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
class Resource{//定义一个操作的资源
	private int num=0;//这个要进行加减操作的数据
	private boolean flag=true;//加减的切换
	//flag=true;表示可以进行加法操作,但无法进行减法操作
	//flag=false;表示可以进行减法操作,但是无法进行加法操作
	public synchronized void add() throws Exception {//执行加法操作
		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 Exception {//执行减法操作
		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();//唤醒全部等待线程
	}
}



在这里插入图片描述
这一题目是经典的多线程开发操作,这个程序里面一定要考虑的核心本质在于:加一个、减一个,整体的计算结果应该只在0、-1、1之间循环出现

生产电脑

设计一个生产电脑和搬运电脑的类,要求生产一台电脑就搬走一台电脑,如果没有新电脑的生产就等待新电脑生产;如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出电脑生产的数量

解答:在本程序之中实现的就是一个标准的生产者与消费者的处理模型

public class ThreadDemo{
	public static void main(String[] args) throws Exception {
		
		Resource res=new Resource();
		new Thread(new Producer(res)).start();
		new Thread(new Consumer(res)).start();
		
	}
}
class Producer implements Runnable{
	private Resource resource;
	public Producer(Resource resource) {
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int x=0;x<50;x++) {
			
			this.resource.make();
		}
		
	}
	
}
class Consumer implements Runnable{
	private Resource resource;
	public Consumer(Resource resource) {
		this.resource=resource;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int x=0;x<50;x++) {
	
			this.resource.get();
		}
		
	}
	
}
class Resource{
	private Computer computer;
	private boolean flag=true;
	public synchronized void make() {
		if(this.computer!=null) {//已经生产过了
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.computer=new Computer("小米电脑",1.1);
		System.out.println("生产电脑"+this.computer);
		super.notifyAll();
	}
	public synchronized void get() {
		if(this.computer==null) {//还没有生产
			try {
				super.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("取走电脑"+this.computer);
		this.computer=null;//已经取走了
		super.notifyAll();
	}
}
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;
		count++;
	}
	public String toString(){
		return "第"+count +"台电脑"+"电脑名字:"+this.name+"、价值:"+this.price;
	}
}


在这里插入图片描述

竞争抢答

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

由于需要牵扯到数据的返回所以使用Callable更简单

package java线程;

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

public class ThreadDemo{
	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());
		
	}
}
class Mythread implements Callable<String>{
	private boolean flag=false;

	@Override
	public String call() throws Exception {
		// TODO Auto-generated method stub
		synchronized (this) {
			if(this.flag==false) {
				this.flag=true;
				return Thread.currentThread().getName()+"抢答成功";
			}
			else {
				return Thread.currentThread().getName()+"抢答失败";
			}
			
		}
	}
	
}

在这里插入图片描述
使用Callable的主要原因是因为Callable拥有返回值方便我们处理

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

样子的木偶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值