导航:首页 > 编程语言 > 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多线程消费者相关的资料

热点内容
plt转pdf 浏览:360
魔兽60宏命令大全 浏览:475
php志愿者网站源码 浏览:872
贸易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