一道关于笔试的多线程题目
生活随笔
收集整理的這篇文章主要介紹了
一道关于笔试的多线程题目
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
四個線程t1,t2,t3,t4,向4個文件中寫入數(shù)據(jù),t1只能寫入1,t2只能寫入2,t3只能寫入3,t4只能寫入4,對4個文件A,B,C,D寫入如下內(nèi)容:
A:123412341234.....
B:234123412341....
C:341234123412....
D:412341234123....
怎么實現(xiàn)同步可以讓線程并行工作?
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;public class Hello
{static boolean b=false;static ReentrantLock lock=new ReentrantLock(); static ReentrantLock lock_2=new ReentrantLock();static Condition condition_for=lock.newCondition();//線程Thread_t1~Thread_t4分別只寫入1~4class Thread_t1 extends Thread{String path;Thread_t1(String path){this.path=path;}public void run(){print(path,1);}}class Thread_t2 extends Thread{String path;Thread_t2(String path){this.path=path;}public void run(){print(path,2);}}class Thread_t3 extends Thread{String path;Thread_t3(String path){this.path=path;}public void run(){print(path,3);}}class Thread_t4 extends Thread{String path;Thread_t4(String path){this.path=path;}public void run(){print(path,4);}}//寫入方法public void print(String path, int i){lock_2.lock();PrintWriter pw=null;try {pw=new PrintWriter(new FileWriter("c:/work/abcd/"+path+".txt",true),true);pw.print(i);} catch (Exception e) {e.printStackTrace();}finally{pw.close();lock_2.unlock();}}//文件類A~Dclass File_A implements Runnable{int one,two,three,four;File_A(int one,int two, int three, int four){this.one=one;this.two=two;this.three=three;this.four=four;}public void run(){for(int x=0;x<100;x++){select("A",one,two,three,four);}}}class File_B implements Runnable{int one,two,three,four;File_B(int one,int two, int three, int four){this.one=one;this.two=two;this.three=three;this.four=four;}public void run(){for(int x=0;x<100;x++){select("B",one,two,three,four);}}}class File_C implements Runnable{int one,two,three,four;File_C(int one,int two, int three, int four){this.one=one;this.two=two;this.three=three;this.four=four;}public void run(){for(int x=0;x<100;x++){select("C",one,two,three,four);}}}class File_D implements Runnable{int one,two,three,four;File_D(int one,int two, int three, int four){this.one=one;this.two=two;this.three=three;this.four=four;}public void run(){for(int x=0;x<100;x++){select("D",one,two,three,four);}}}
//選擇線程方法,當(dāng)前只允許進入一個文件線程public void select(String path,int one,int two, int three, int four){lock.lock();try{while(b){condition_for.await();}b=true;int[] order={one,two,three,four};//將傳入的1234,換成數(shù)組方便用for遍歷for(int i:order){switch(i){case 1:Thread_t1 t1= new Thread_t1(path);t1.start();t1.join(); //t1.join()是等線程t1執(zhí)行完后再執(zhí)行當(dāng)前線程break;case 2:Thread_t2 t2= new Thread_t2(path);t2.start();t2.join();break;case 3:Thread_t3 t3= new Thread_t3(path);t3.start();t3.join();break;case 4:Thread_t4 t4= new Thread_t4(path);t4.start();t4.join();break;} }b=false;}catch(Exception e){e.printStackTrace();}finally{lock.unlock();}}//主函數(shù)public static void main(String args[]){HelloJava hj=new HelloJava();new Thread(hj.new File_A(1,2,3,4)).start();new Thread(hj.new File_B(2,3,4,1)).start();new Thread(hj.new File_C(3,4,1,2)).start();new Thread(hj.new File_D(4,1,2,3)).start();}
}
總結(jié)
以上是生活随笔為你收集整理的一道关于笔试的多线程题目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Crypto.PublicKey.R
- 下一篇: 推荐一篇关于java集合的博文,写的很n