A. java 程序中怎么保证多线程的运行安全
并发编程三要素(线程的安全性问题体现在):
原子性:原子,即一个不可再被分割的颗粒。原子性指的是一个或多个操作要么 全部执行成功要么全部执行失败。
可见性:一个线程对共享变量的修改,另一个线程能够立刻看到。 (synchronized,volatile)
有序性:程序执行的顺序按照代码的先后顺序执行。(处理器可能会对指令进行 重排序)
出现线程安全问题的原因:
线程切换带来的原子性问题
缓存导致的可见性问题
编译优化带来的有序性问题
解决办法:
JDK Atomic开头的原子类、synchronized、LOCK,可以解决原子性问题
synchronized、volatile、LOCK,可以解决可见性问题
Happens-Before 规则可以解决有序性问题
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