最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java線程:火車站購(gòu)票,銀行存錢,收取停車費(fèi),模擬員工制作工藝品【詩書畫唱】

2020-07-24 23:48 作者:詩書畫唱  | 我要投稿

使用多線程模擬火車站購(gòu)票問題


package thread;


public class huoCheZhan {



public static void main(String[] args) {

//用New創(chuàng)建多個(gè)窗口對(duì)象:


TicketClass TicketClass=new TicketClass();


Thread Thread1=new Thread(TicketClass);


Thread1.setName("窗口1");


Thread Thread2=new Thread(TicketClass);


Thread2.setName("窗口2");


Thread Thread3=new Thread(TicketClass);


Thread3.setName("窗口3");


//用start啟動(dòng)多線程對(duì)象:


Thread1.start();


Thread2.start();


Thread3.start();


}


}


class TicketClass implements Runnable {

int num=1;

int zongPiaoShu=99;


public void run() {


while(true) {


if(zongPiaoShu>0) {

synchronized (this) {

try {


Thread.sleep(100);


} catch (InterruptedException e) {


// TODO 自動(dòng)生成的 catch 塊


e.printStackTrace();


}

if(zongPiaoShu>=0){

System.out.println(Thread.currentThread()

.getName()+"買出了第"+(num++)+"張票, 還剩"+(zongPiaoShu--)+"張票沒買出去");

}

}

}

}


}


}











模擬用戶去銀行存錢

/**

?* 需求:有兩個(gè)儲(chǔ)戶同時(shí)到一個(gè)銀行去存錢,每人存3次,

?* 每次存100元。

?* 要求打印出銀行每次金額的變化情況(假設(shè)銀行沒錢)

?*?

?* 分析:得出有兩個(gè)類:銀行和儲(chǔ)戶

?* 這是一個(gè)多線程的問題,如何分析他的線程安全問題呢?

?* 1.通過線程的任務(wù)代碼分析。

?* 2.有沒有共享數(shù)據(jù),有沒有多條語句在操作共享數(shù)據(jù)。

?* */




package thread;


class Bank{

// moneySum:錢的總數(shù)

private int moneySum = 0;

// cunRuMoneyNum:每次存入的錢

public void add(int cunRuMoneyNum){

moneySum = moneySum + cunRuMoneyNum;

System.out.println(Thread.currentThread()

.getName()+

"向銀行存了100元,銀行現(xiàn)在有"

+moneySum+"元");

}

}

?

class Customer implements Runnable{

?

Bank yingHang = new Bank();

Object guKe = new Object();

?

@Override

public void run() {

synchronized (guKe) {

for(int i = 0;i<3;i++){

yingHang.add(100);

// 100:每次存入的100元錢

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

?

public class blank {

public static void main(String[] args) {

Customer CustomerClass = new Customer();


Thread Thread1 =?

new Thread(CustomerClass,"詩書畫唱");

Thread Thread2 =

new Thread(CustomerClass,"三連關(guān)注");


Thread1.start();

Thread2.start();

}

}









小區(qū)收取停車費(fèi)


package thread;


class ParkClass{

// moneySum:錢的總數(shù)

private int moneySum = 0;

// cunRuMoneyNum:每次停車交的錢

public void add(int cunRuMoneyNum){

moneySum = moneySum + cunRuMoneyNum;

System.out.println(Thread.currentThread()

.getName()+

"停車了,交100元 ,停車場(chǎng)現(xiàn)在有"

+moneySum+"元");

}

}

?

class CarCLass implements Runnable{

?

parkChangClass parkChangClass

= new parkChangClass();

Object carZhuRen = new Object();

// carZhuRen:車的主人

@Override

public void run() {

synchronized (carZhuRen) {

for(int i = 0;i<3;i++){

parkChangClass.add(100);

// 100:每次存入的100元錢

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

?

public class tingChe {

public static void main(String[] args) {

Customer car = new Customer();


Thread Thread1 =?

new Thread(car,"詩書畫唱車");

Thread Thread2 =

new Thread(car,"三連關(guān)注車");

Thread Thread3 =?

new Thread(car,"6666車");

Thread Thread4 =

new Thread(car,"2333車");

Thread1.start();

Thread2.start();

Thread3.start();

Thread4.start();

}

}






模擬員工制作工藝品,工藝品數(shù)量達(dá)到1000個(gè)以后不再制作



package thread;

import java.util.Random;

public class yuanGong {


public static void main(String[] args) throws Exception{


gongYiPinClass gongYiPinClass=new gongYiPinClass() ;


// 下面是省了重命名的部分,直接new實(shí)例化:

new Thread(gongYiPinClass, "詩書畫唱的好員工").start();

new Thread(gongYiPinClass, "三連關(guān)注的好員工").start();

new Thread(gongYiPinClass,"2333的好員工").start();


}

}


class gongYiPinClass implements Runnable{

static int gongYiPinNum=999;

static int num=1;

public void run() {


while(gongYiPinNum>0){


synchronized (this) {


try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}



if(gongYiPinNum>=0){

System.out.println(Thread.currentThread()

.getName()+"做出了第"+(num++)+"個(gè)工藝品,"

+ "\n還有"+(gongYiPinNum--)+"個(gè)工藝品沒做完");

}

// gongYiPinNum--;

}

}

}


}





用Java多線程模擬停車場(chǎng)停車和停車場(chǎng)滿了,車離開等的情形:

package thread;

?

class Park

{


// park:停車場(chǎng)

boolean? []park=new boolean[3];

public boolean equals()

{

return true;

}


}

class Car

{

private String number;

// position:位置

private int position=0;

public Car(String number)

{

this.number=number;

}

public void setNumber(String number)

{

this.number=number;

}

public String getNumber()

{

return number;

}

public void setPosition(int i)

{?

position=i;

}

public int getPosition()

{

return position;

}

public int hashCode()

{

return number.hashCode();

}


// duiXiang:車等的對(duì)象

public boolean equals(Object duiXiang)

{

if(duiXiang==this)

return true;

if(duiXiang!=null&&duiXiang.getClass()

==Car.class)

{

Car Car=(Car)duiXiang;

return Car.number==this.number;

}

return false;

}

}

?

class ParkCar implements Runnable

{

Car Car;

Park Park;

public ParkCar(Car Car,Park Park)

{

this.Car=Car;

this.Park=Park;

}

@Override

public void run()

{

while(true)

{

int i=0;

synchronized(Park)

{

if(Car.getPosition()==0)

{

for(i=0;i<Park.park.length;i++)

{

if(Park.park[i])

{

continue;

}

else

{

System.out.println(Car.getNumber()+"車,成功停在"

+(i+1)+"號(hào)停車位上");

Park.park[i]=true;

Car.setPosition(i);

break;

}

}

if(i==3)

System.out.println

(Car.getNumber()+"車,停車失敗"

+",因?yàn)橥\噲?chǎng)車位已滿 "

);

}



try

{

Thread.sleep(100);

}

catch(InterruptedException ie)

{

ie.printStackTrace();

}

}


synchronized(Park)

{

if(Car.getPosition()!=0)

{

Park.park[Car.getPosition()]=false;

Car.setPosition(0);

System.out.println(Car.getNumber()

+"車,已離開停車場(chǎng)");


}

try

{

Thread.sleep(100);

}

catch(InterruptedException ie)

{

ie.printStackTrace();

}

}


}

}

}

public class tingCheChang {

public static void main(String []args)

{

Park Park=new Park();

Car Car1=new Car("詩書畫唱號(hào)");

Car Car2=new Car("2333號(hào)");

Car Car3=new Car("6666號(hào)");

Car Car4=new Car("8888號(hào)");

Car Car5=new Car("三連關(guān)注號(hào)");

new Thread (new ParkCar(Car1,Park)).start();

new Thread (new ParkCar(Car3,Park)).start();

new Thread (new ParkCar(Car4,Park)).start();

new Thread (new ParkCar(Car5,Park)).start();

new Thread (new ParkCar(Car2,Park)).start();


}

?

}




Java線程:火車站購(gòu)票,銀行存錢,收取停車費(fèi),模擬員工制作工藝品【詩書畫唱】的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
筠连县| 进贤县| 楚雄市| 开原市| 榆树市| 肥乡县| 玛沁县| 大姚县| 高淳县| 南涧| 金沙县| 永川市| 分宜县| 法库县| 游戏| 长寿区| 黄大仙区| 庆城县| 冷水江市| 兰溪市| 鄂尔多斯市| 乳源| 肇庆市| 汾阳市| 临澧县| 新河县| 茂名市| 铁岭市| 赣榆县| 宜君县| 绥化市| 深圳市| 南涧| 杂多县| 霍州市| 孝昌县| 东乡族自治县| 西畴县| 西贡区| 清水河县| 襄城县|