软件编程
位置:首页>> 软件编程>> java编程>> Java多线程编程综合案例详解

Java多线程编程综合案例详解

作者:样子的木偶  发布时间:2023-12-09 18:13:25 

标签: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();//唤醒全部等待线程
   }
}

Java多线程编程综合案例详解

这一题目是经典的多线程开发操作,这个程序里面一定要考虑的核心本质在于:加一个、减一个,整体的计算结果应该只在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;
   }
}

Java多线程编程综合案例详解

竞争抢答

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

由于需要牵扯到数据的返回所以使用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()+"抢答失败";
}

}
}

}

Java多线程编程综合案例详解

使用Callable的主要原因是因为Callable拥有返回值方便我们处理

来源:https://blog.csdn.net/m0_63694520/article/details/125952930

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com