欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

一道关于笔试的多线程题目

发布时间:2024/9/30 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 一道关于笔试的多线程题目 小编觉得挺不错的,现在分享给大家,帮大家做个参考.
四个线程t1,t2,t3,t4,向4个文件中写入数据,t1只能写入1,t2只能写入2,t3只能写入3,t4只能写入4,对4个文件A,B,C,D写入如下内容: A:123412341234..... B:234123412341.... C:341234123412.... D:412341234123.... 怎么实现同步可以让线程并行工作? 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);}}} //选择线程方法,当前只允许进入一个文件线程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,换成数组方便用for遍历for(int i:order){switch(i){case 1:Thread_t1 t1= new Thread_t1(path);t1.start();t1.join(); //t1.join()是等线程t1执行完后再执行当前线程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();}}//主函数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();} }

总结

以上是生活随笔为你收集整理的一道关于笔试的多线程题目的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。