您的位置:首页 > 其它

多线程---有四个线程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文件

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;
}
}
}
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: