導航:首頁 > 編程語言 > java多線程消費者

java多線程消費者

發布時間:2025-01-22 03:44:36

A. java 程序中怎麼保證多線程的運行安全

並發編程三要素(線程的安全性問題體現在):

原子性:原子,即一個不可再被分割的顆粒。原子性指的是一個或多個操作要麼 全部執行成功要麼全部執行失敗。

可見性:一個線程對共享變數的修改,另一個線程能夠立刻看到。 (synchronized,volatile)

有序性:程序執行的順序按照代碼的先後順序執行。(處理器可能會對指令進行 重排序)

出現線程安全問題的原因:

B. java多線程關於消費者和生產者,求源程序,求大神解答。願意提高報酬

自己看代碼體會吧

importjava.util.concurrent.BlockingQueue;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importjava.util.concurrent.LinkedBlockingQueue;

publicclassBlockingQueueTest{
publicstaticvoidmain(String[]args){
ExecutorServiceservice=Executors.newCachedThreadPool();
BlockingQueue<String>blockingQueue=newLinkedBlockingQueue<String>(100);
System.out.println("blockingQueuenowcontains"+blockingQueue.size()+"unit");
service.submit(newConsumer1(blockingQueue));
gap(blockingQueue);
service.submit(newProctor2(blockingQueue));
gap(blockingQueue);
service.submit(newProctor3(blockingQueue));
gap(blockingQueue);
service.submit(newProctor4(blockingQueue));
gap(blockingQueue);
service.submit(newProctor5(blockingQueue));
gap(blockingQueue);

service.shutdown();
}
privatestaticvoidgap(BlockingQueue<String>blockingQueue){
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
System.out.println("blockingQueuenowcontains"+blockingQueue.size()+"unit");
}
}
{
BlockingQueue<String>blockingQueue;

publicConsumer1(BlockingQueue<String>blockingQueue){
super();
this.blockingQueue=blockingQueue;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
System.out.println("Consumer1start:need10units");
for(inti=0;i<10;i++){
try{
blockingQueue.take();
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("Consumer1end:hasgot10units");
}

}
{
BlockingQueue<String>blockingQueue;

publicProctor2(BlockingQueue<String>blockingQueue){
super();
this.blockingQueue=blockingQueue;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
System.out.println("Proctor2start:put5units");
for(inti=0;i<5;i++){
try{
blockingQueue.put("Object");
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("Proctor2end:hasput5units");
}

}
{
BlockingQueue<String>blockingQueue;

publicProctor3(BlockingQueue<String>blockingQueue){
super();
this.blockingQueue=blockingQueue;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
System.out.println("Proctor3start:put5units");
for(inti=0;i<5;i++){
try{
blockingQueue.put("Object");
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("Proctor3end:hasput5units");
}

}
{
BlockingQueue<String>blockingQueue;

publicProctor4(BlockingQueue<String>blockingQueue){
super();
this.blockingQueue=blockingQueue;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
System.out.println("Proctor4start:put100units");
for(inti=0;i<100;i++){
try{
blockingQueue.put("Object");
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("Proctor4end:hasput100units");
}

}
{
BlockingQueue<String>blockingQueue;

publicProctor5(BlockingQueue<String>blockingQueue){
super();
this.blockingQueue=blockingQueue;
}
@Override
publicvoidrun(){
//TODOAuto-generatedmethodstub
System.out.println("Proctor5start:put10units");
for(inti=0;i<100;i++){
try{
blockingQueue.put("Object");
}catch(InterruptedExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
System.out.println("Proctor5end:hasput10units");
}

}

每個線程是隔了1s啟動的, 結果

blockingQueue now contains 0 unit
Consumer1 start: need 10 units
blockingQueue now contains 0 unit
Proctor2 start: put 5 units
Proctor2 end: has put 5 units
blockingQueue now contains 0 unit
Proctor3 start: put 5 units
Proctor3 end: has put 5 units
Consumer1 end: has got 10 units
blockingQueue now contains 0 unit
Proctor4 start: put 100 units
Proctor4 end: has put 100 units
blockingQueue now contains 100 unit
Proctor5 start: put 10 units
blockingQueue now contains 100 unit

閱讀全文

與java多線程消費者相關的資料

熱點內容
魔獸60宏命令大全 瀏覽:473
php志願者網站源碼 瀏覽:870
貿易pdf 瀏覽:495
dbug命令 瀏覽:351
開逛app如何加好友 瀏覽:958
ftpdos命令下載文件 瀏覽:75
華為如何打開語音伺服器 瀏覽:242
python中的idle 瀏覽:1000
五軸聯動數控編程 瀏覽:965
換一台電腦如何遠程雲伺服器 瀏覽:132
阿里雲怎麼買雲伺服器 瀏覽:664
java提取文字 瀏覽:97
阿里雲伺服器同人賬號問題 瀏覽:420
5分鍾解壓軸題 瀏覽:341
安卓桌面二級文件夾 瀏覽:188
eps文檔加密 瀏覽:261
手機怎麼做pdf 瀏覽:162
ug曲面pdf 瀏覽:279
液化氣還是壓縮氣 瀏覽:950
阿里雲公共ntp伺服器地址 瀏覽:991