最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java多線程編程綜合案例詳解

 更新時間:2022年07月26日 14:05:08   作者:樣子的木偶  
這篇文章將通過三個案例帶大家了解一下Java中的多線程編程,文中的示例代碼介紹詳細(xì),對我們的學(xué)習(xí)或工作有一定的價值,感興趣的小伙伴可以了解一下

Java多線程綜合案例

數(shù)字加減

設(shè)計(jì)4個線程對象,兩個線程執(zhí)行減操作,兩個線程執(zhí)行加操作

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;//這個要進(jìn)行加減操作的數(shù)據(jù)
    private boolean flag=true;//加減的切換
    //flag=true;表示可以進(jìn)行加法操作,但無法進(jìn)行減法操作
    //flag=false;表示可以進(jìn)行減法操作,但是無法進(jìn)行加法操作
    public synchronized void add() throws Exception {//執(zhí)行加法操作
        if(this.flag==false) {//線程需要執(zhí)行的是減法操作,加法操作要等待處理
            super.wait();
        }
        Thread.sleep(100);
        this.num++;
        System.out.println("加法操作-"+Thread.currentThread().getName()+"num="+this.num);
        this.flag=false;//加法操作執(zhí)行完畢,需要執(zhí)行減法處理
        super.notifyAll();//喚醒全部等待處理
    }
    public synchronized void sub() throws Exception {//執(zhí)行減法操作
        if(this.flag==true) {//線程需要執(zhí)行的是加法操作,減法操作要等待處理
            super.wait();
        }
        Thread.sleep(200);
        this.num--;
        System.out.println("減法操作-"+Thread.currentThread().getName()+"num="+this.num);
        this.flag=true;//減法操作執(zhí)行完畢,現(xiàn)在要執(zhí)行加法操作
        super.notifyAll();//喚醒全部等待線程
    }
}

這一題目是經(jīng)典的多線程開發(fā)操作,這個程序里面一定要考慮的核心本質(zhì)在于:加一個、減一個,整體的計(jì)算結(jié)果應(yīng)該只在0、-1、1之間循環(huán)出現(xiàn)

生產(chǎn)電腦

設(shè)計(jì)一個生產(chǎn)電腦和搬運(yùn)電腦的類,要求生產(chǎn)一臺電腦就搬走一臺電腦,如果沒有新電腦的生產(chǎn)就等待新電腦生產(chǎn);如果生產(chǎn)出的電腦沒有搬走,則要等待電腦搬走之后再生產(chǎn),并統(tǒng)計(jì)出電腦生產(chǎn)的數(shù)量

解答:在本程序之中實(shí)現(xiàn)的就是一個標(biāo)準(zhǔn)的生產(chǎn)者與消費(fèi)者的處理模型

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) {//已經(jīng)生產(chǎn)過了
            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("生產(chǎn)電腦"+this.computer);
        super.notifyAll();
    }
    public synchronized void get() {
        if(this.computer==null) {//還沒有生產(chǎn)
            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;//已經(jīng)取走了
        super.notifyAll();
    }
}
class Computer{
    private static int count=0;//表示生產(chǎn)個數(shù)
    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;
    }
}

競爭搶答

實(shí)現(xiàn)一個競拍搶答程序:要求設(shè)置三個搶答者(三個線程),而后發(fā)出搶答指令,搶答成功給出搶答成功提示,搶答失敗給出搶答失敗提示

由于需要牽扯到數(shù)據(jù)的返回所以使用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的主要原因是因?yàn)镃allable擁有返回值方便我們處理

到此這篇關(guān)于Java多線程編程綜合案例詳解的文章就介紹到這了,更多相關(guān)Java多線程編程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java啟動參數(shù)之謎的排查過程

    java啟動參數(shù)之謎的排查過程

    在日常操作中,相信很多人對Java啟動參數(shù)存在疑惑,下面這篇文章主要給大家介紹了關(guān)于java啟動參數(shù)之謎的排查過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • 在SpringBoot中靈活使用異步事件的操作步驟

    在SpringBoot中靈活使用異步事件的操作步驟

    SpringBoot中的異步事件機(jī)制通過事件源和事件監(jiān)聽器的解耦,實(shí)現(xiàn)了松耦合的組件間通信,事件源發(fā)布事件,監(jiān)聽器異步處理事件,本文介紹在SpringBoot中靈活使用異步事件的操作,感興趣的朋友一起看看吧
    2025-03-03
  • Spring MVC學(xué)習(xí)之DispatcherServlet請求處理詳析

    Spring MVC學(xué)習(xí)之DispatcherServlet請求處理詳析

    這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之DispatcherServlet請求處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • 詳解Java中的字節(jié)碼增強(qiáng)技術(shù)

    詳解Java中的字節(jié)碼增強(qiáng)技術(shù)

    字節(jié)碼增強(qiáng)技術(shù)就是一類對現(xiàn)有字節(jié)碼進(jìn)行修改或者動態(tài)生成全新字節(jié)碼文件的技術(shù)。本文將通過示例詳細(xì)說說Java的字節(jié)碼增強(qiáng)技術(shù),需要的可以參考一下
    2022-10-10
  • 實(shí)例解析如何正確使用Java數(shù)組

    實(shí)例解析如何正確使用Java數(shù)組

    同一種類型數(shù)據(jù)的集合。其實(shí)數(shù)組就是一個容器。運(yùn)算的時候有很多數(shù)據(jù)參與運(yùn)算,那么首先需要做的是什么下面我們就一起來看看。
    2016-07-07
  • Java設(shè)計(jì)模式之迭代器模式

    Java設(shè)計(jì)模式之迭代器模式

    這篇文章介紹了Java設(shè)計(jì)模式之迭代器模式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • 淺談JSONObject的使用及示例代碼(JSON解析)

    淺談JSONObject的使用及示例代碼(JSON解析)

    這篇文章主要介紹了淺談JSONObject的使用及示例代碼(JSON解析),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法

    java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法

    今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 建議你使用LocalDateTime而不是Date哦

    建議你使用LocalDateTime而不是Date哦

    這篇文章主要介紹了建議你使用LocalDateTime而不是Date,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • springboot解決前后端分離時的跨域問題

    springboot解決前后端分離時的跨域問題

    這篇文章主要介紹了springboot如何解決前后端分離時的跨域問題,幫助大家更好的理解和學(xué)習(xí)使用springboot,感興趣的朋友可以了解下
    2021-04-04

最新評論

大安市| 赞皇县| 云南省| 江源县| 呼和浩特市| 九台市| 铁岭县| 江源县| 舟曲县| 十堰市| 敖汉旗| 游戏| 南和县| 鸡泽县| 黄平县| 武隆县| 余干县| 台中市| 遵义县| 建瓯市| 托克逊县| 偏关县| 永平县| 临沭县| 彩票| 射洪县| 开鲁县| 大足县| 布尔津县| 南平市| 临清市| 镇雄县| 永新县| 治多县| 霍林郭勒市| 瓦房店市| 山东省| 南雄市| 黄陵县| 巴彦县| 闻喜县|