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