Java多線程編程綜合案例詳解
Java多線程綜合案例
數(shù)字加減
設(shè)計(jì)4個(gè)線程對象,兩個(gè)線程執(zhí)行減操作,兩個(gè)線程執(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{//定義一個(gè)操作的資源
private int num=0;//這個(gè)要進(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ā)操作,這個(gè)程序里面一定要考慮的核心本質(zhì)在于:加一個(gè)、減一個(gè),整體的計(jì)算結(jié)果應(yīng)該只在0、-1、1之間循環(huán)出現(xiàn)
生產(chǎn)電腦
設(shè)計(jì)一個(gè)生產(chǎn)電腦和搬運(yùn)電腦的類,要求生產(chǎn)一臺電腦就搬走一臺電腦,如果沒有新電腦的生產(chǎn)就等待新電腦生產(chǎn);如果生產(chǎn)出的電腦沒有搬走,則要等待電腦搬走之后再生產(chǎn),并統(tǒng)計(jì)出電腦生產(chǎn)的數(shù)量
解答:在本程序之中實(shí)現(xiàn)的就是一個(gè)標(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)個(gè)數(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+"、價(jià)值:"+this.price;
}
}
競爭搶答
實(shí)現(xiàn)一個(gè)競拍搶答程序:要求設(shè)置三個(gè)搶答者(三個(gè)線程),而后發(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)文章
JMS簡介與ActiveMQ實(shí)戰(zhàn)代碼分享
這篇文章主要介紹了JMS簡介與ActiveMQ實(shí)戰(zhàn)代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
SpringBoot中實(shí)現(xiàn)訂單30分鐘自動取消的三種方案分享
在電商和其他涉及到在線支付的應(yīng)用中,通常需要實(shí)現(xiàn)一個(gè)功能:如果用戶在生成訂單后的一定時(shí)間內(nèi)未完成支付,系統(tǒng)將自動取消該訂單,本文將詳細(xì)介紹基于Spring Boot框架實(shí)現(xiàn)訂單30分鐘內(nèi)未支付自動取消的幾種方案,并提供實(shí)例代碼,需要的朋友可以參考下2023-10-10
舉例解析Java多線程編程中需要注意的一些關(guān)鍵點(diǎn)
這篇文章主要介紹了Java多線程編程中需要注意的一些關(guān)鍵點(diǎn),包括ThreadLocal變量與原子更新等一些深層次的內(nèi)容,需要的朋友可以參考下2015-11-11
基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取?
這篇文章主要介紹了基于Java解決華為機(jī)試實(shí)現(xiàn)密碼截取,文章圍繞主題相關(guān)資料展開詳細(xì)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下,希望對你有所幫助2022-02-02

