多线程---有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD

有四个线程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;
                }
            }
        }
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值