码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-07-05 11:05:23      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:多线程   面试   google   四个线程   四个文件   

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

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:多线程   面试   google   四个线程   四个文件   

原文地址:http://blog.csdn.net/havedream_one/article/details/46761487

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!