Java多线程编程综合案例详解
作者:样子的木偶 时间:2023-12-09 18:13:25
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拥有返回值方便我们处理
来源:https://blog.csdn.net/m0_63694520/article/details/125952930
标签:Java,多线程,编程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入理解java动态代理的两种实现方式(JDK/Cglib)
2023-11-26 13:29:52
详解如何在Android studio中更新sdk版本和build-tools版本
2023-11-18 04:36:15
![](https://img.aspxhome.com/file/2023/1/91031_0s.png)
C#入门教程之集合ArrayList用法详解
2022-04-30 06:32:13
![](https://img.aspxhome.com/file/2023/8/119568_0s.png)
C#敏感词过滤实现方法
2022-11-28 12:00:31
![](https://img.aspxhome.com/file/2023/4/129654_0s.png)
Android EditText密码的隐藏和显示功能
2021-11-17 16:49:47
![](https://img.aspxhome.com/file/2023/8/89508_0s.png)
简单了解Spring中BeanFactory与FactoryBean的区别
2022-01-14 03:59:20
C#不重复输出一个数组中所有元素的方法
2022-07-02 14:25:58
![](https://img.aspxhome.com/file/2023/5/68085_0s.png)
Android图片加载利器之Picasso扩展功能
2023-05-14 15:12:33
![](https://img.aspxhome.com/file/2023/3/122103_0s.jpg)
asp.net之生成验证码的方法集锦(一)
2022-09-07 22:37:13
Android中Property模块的键值设置
2021-10-14 08:40:25
C#无损压缩图片
2022-05-26 22:54:39
idea使用外置tomcat配置springboot详细步骤
2023-11-20 03:56:49
![](https://img.aspxhome.com/file/2023/8/98108_0s.png)
C# 下载文件 删除文件 写入文本的实例
2022-11-27 18:34:25
C#实现多线程的同步方法实例分析
2021-11-01 11:56:42
C++异常处理 try,catch,throw,finally的用法
2021-08-21 21:29:55
Java中五种不同方法的创建对象
2021-07-25 01:11:37
springboot整合vue实现上传下载文件
2023-11-14 07:10:37
![](https://img.aspxhome.com/file/2023/4/94374_0s.jpg)
C#并行编程Task类用法介绍
2021-06-26 20:58:17
Springboot集成RabbitMQ死信队列的实现
2022-08-24 13:10:54
![](https://img.aspxhome.com/file/2023/9/77479_0s.png)
java8中的默认垃圾回收器(GC)
2021-12-01 04:27:30
![](https://img.aspxhome.com/file/2023/2/86842_0s.png)