有2个文件,a.txt,b.txt,分别有10行内容,现要求一行一行读取a.txt,b.txt。将读取的内容按顺序写到到c.txt文件中。
比如a.txt内容为:
1
3
5
7
9
b.txt内容为:
2
4
6
8
输入到c.txt的内容为:
1
2
3
4
5
6
7
8
9求如何编写

解决方案 »

  1.   

    这根本不是多线程问题,用两个list记录内容,合并排序
      

  2.   

    可以考虑用公平锁的方式来处理.
    当然也可以考虑通过定义一个标志flag来让一个线程wait,并触发另一个线程运行的方式来处理上面的这个问题.
    一下是用公平锁的方式import java.util.concurrent.locks.ReentrantLock;public class Test{

    public static void main(String[] args){
    ReentrantLock lock = new ReentrantLock(true);
    String[] strs1 = {"1","3","5","7","9"};
    String[] strs2 = {"2","4","6","8","10"};
    Thread thread1 = getThread(strs1,lock);
    Thread thread2 = getThread(strs2,lock);
    thread1.start();
    try{
    Thread.sleep(10);
    }catch(Exception e){
    e.printStackTrace();
    }
    thread2.start();
    }
    public static Thread getThread(String[] strs,ReentrantLock lock){
    Thread thread = new Thread(){
    @Override
    public void run(){
    for(int i = 0 ; i < strs.length ; i ++){
    lock.lock();
    System.out.println(strs[i]);
    lock.unlock();
    try{
    Thread.sleep(20);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    }; return thread;
    }
    }
      

  3.   

        public final String FILE_END="FILE_END";    @Test
        public void test() throws IOException {
            FileReader f1 = new FileReader(FileTest.class.getResource("a.txt").getFile());
            FileReader f2 = new FileReader(FileTest.class.getResource("b.txt").getFile());
            final FileWriter f3 = new FileWriter(FileTest.class.getResource("c.txt").getFile());
            final LinkedBlockingDeque<String> q1=new LinkedBlockingDeque<String>();
            final LinkedBlockingDeque<String> q2=new LinkedBlockingDeque<String>();
            System.out.println(FileTest.class.getResource("c.txt").getFile());
            getReadThread(f1,q1,"t--1").start();
            getReadThread(f2,q2,"t--2").start();
            getWriteThread(f3,q1,q2,"t--3").start();
            while (Thread.activeCount()>2){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        public Thread getWriteThread(final FileWriter f, final LinkedBlockingDeque<String> q1,final  LinkedBlockingDeque<String> q2,String name){
            return new Thread(new Runnable() {
                BufferedWriter bw=new BufferedWriter(f);
                @Override
                public void run() {
                    try {
                        boolean go1=true;
                        boolean go2=true;
                        while (go1||go2) {
                            if(go1){
                                String c1=q1.take();
                                if(c1==FILE_END){
                                    go1=false;
                                }else {
                                    bw.write(c1);
                                    bw.newLine();
                                }
                            }
                            if(go2){
                                String c2=q2.take();
                                if(c2==FILE_END){
                                    go2=false;
                                }else {
                                    bw.write(c2);
                                    bw.newLine();
                                }
                            }
                        }
                        System.out.println("=======***********======");
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            bw.close();
                            f.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            },name);
        }
        public Thread getReadThread(final FileReader f, final LinkedBlockingDeque<String> q,String name){
            return new Thread(new Runnable() {
                @Override
                public void run() {
                    BufferedReader bufferedReader=new BufferedReader(f);
                    String line=null;
                    try {
                        while ((line=bufferedReader.readLine())!=null){
                            q.add(line);
                        }
                        q.add(FILE_END);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            bufferedReader.close();
                            f.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            },name);
        }