多线程---有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD
2015-07-05 09:54
716 查看
有四个线程1、2、3、4。
线程1的功能就是输出1,
线程2的功能就是输出2,以此类推………现在有四个文件ABCD。
初始都为空。
现要让四个文件呈如下格式:
A:1 2 3 4 1 2….
B:2 3 4 1 2 3….
C:3 4 1 2 3 4….
D:4 1 2 3 4 1….
请设计程序。
先试着写出四个线程交替写入A文件
扩展到四个文件
为任务类指定id和name
id表示打印的顺序
name表示打印时打印的字符
id需要根据情况在文件类里时刻调整顺序
线程1的功能就是输出1,
线程2的功能就是输出2,以此类推………现在有四个文件ABCD。
初始都为空。
现要让四个文件呈如下格式:
A:1 2 3 4 1 2….
B:2 3 4 1 2 3….
C:3 4 1 2 3 4….
D:4 1 2 3 4 1….
请设计程序。
先试着写出四个线程交替写入A文件
public class FourThreadOneFile { public static FILE file = new FILE(); public static void main(String[] args) { System.out.println("Hello World!"); ExecutorService executors = Executors.newFixedThreadPool(5); executors.execute(new PrintTask(1)); executors.execute(new PrintTask(2)); executors.execute(new PrintTask(3)); executors.execute(new PrintTask(4)); executors.shutdown(); } static class PrintTask implements Runnable { private int id = 0; public PrintTask(int id){ this.id = id; } public void run(){ while(true){ file.print(id); } } } } class FILE { //private static Lock = new ReentrantLock(); private static int state = 0; private static PrintWriter out; public FILE(){ try{ out = new PrintWriter("A.txt"); }catch(Exception fileNotFound){ } } public static synchronized void print(int id) { if(state == (id - 1)){ try{ out.print(id); System.out.println(id); Thread.sleep(1000); }catch(InterruptedException ex1){ }finally{ //刷新缓冲区 out.flush(); } state++; if(state == 4){ out.println(); state = 0; } } } }
扩展到四个文件
为任务类指定id和name
id表示打印的顺序
name表示打印时打印的字符
id需要根据情况在文件类里时刻调整顺序
public class FourThreadFourFile { public static FILE file = new FILE(); public static void main(String[] args) throws Exception { System.out.println("Hello World!"); PrintWriter o = new PrintWriter("B"); o.println("hello"); o.close(); ExecutorService executors = Executors.newFixedThreadPool(5); executors.execute(new PrintTask(1,"1")); executors.execute(new PrintTask(2,"2")); executors.execute(new PrintTask(3,"3")); executors.execute(new PrintTask(4,"4")); executors.shutdown(); } public static class PrintTask implements Runnable { private int id = 0; public String name; public PrintTask(int id,String name){ this.id = id; this.name = name; } public void run(){ while(true){ file.printFile(id,this); } } } } class FILE { //private static Lock = new ReentrantLock(); //代表需要打印的数 private static int state = 0; //选择操作文件 //0---A //1---B //2---C //3---D private static int select = 0; private static PrintWriter outA; private static PrintWriter outB; private static PrintWriter outC; private static PrintWriter outD; private static PrintWriter out; public FILE(){ try{ outA = new PrintWriter("A.txt"); outB = new PrintWriter("B.txt"); outC = new PrintWriter("C.txt"); outD = new PrintWriter("D.txt"); }catch(Exception fileNotFound){ } } public static synchronized void printFile(int id,FourThreadFourFile.PrintTask pt){ switch(select){ case 0: out = outA; print(id,pt); break; case 1: out = outB; //调整id id = id - 1; if(id <= 0){ id += 4; } print(id,pt); break; case 2: out = outC; //调整id id = id - 2; if(id <= 0){ id += 4; } print(id,pt); break; case 3: out = outD; id = id - 3; //调整id if(id <= 0){ id += 4; } print(id,pt); break; } } public static synchronized void print(int id,FourThreadFourFile.PrintTask pt) { if(state == (id - 1)){ try{ out.print(pt.name); System.out.println((char)('A'+select)+"-----" + pt.name); Thread.sleep(1000); }catch(InterruptedException ex1){ }finally{ //刷新缓冲区 out.flush(); } state++; if(state == 4){ out.println(); state = 0; select++; if(select == 4){ select = 0; } } } } }
相关文章推荐
- FZU 1845 Look Up(单调栈)
- 在django template中设置临时变量
- Android应用发布后的统计——百度移动统计的应用
- PHP面试题遇到的几个坑。...面壁ing
- FPGA中modelsim对IP的仿真
- 30、Java中Set集合之HashSet、TreeSet和EnumSet
- C# 类型基础
- 代理模式,JDK动态代理
- [linux]如何安装.bin类型的文件
- YII1.0中验证码刷新不更新的问题的解决。
- FPGA中对FIFO深度的计算
- js实现的省市联动
- 创建常量、原子性的值类型
- 微软小冰发威,招摇人工智能
- JAVA设计模式-辛格尔顿
- 使用Spring容器
- pandas+numpy ufunc重写question2计算位温
- 七大排序实测(计数、快速、归并、大堆、希尔、插入、选择)
- Linux(centos 6.5) 调用java脚本以及定时执行的脚本实例及配置文件详解
- HBase在线数据备份