java多线程问题

???????????864 2018-04-07 01:55:30
有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

求如何编写
...全文
479 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
建两个文件流。每个文件读一行,合并到一个文件里就行了。不用那么复杂吧?
LGX_TvT 2018-04-10
  • 打赏
  • 举报
回复
同步锁来一下不就好了- -
岂是蓬蒿人 2018-04-10
  • 打赏
  • 举报
回复
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); }
rickylin86 2018-04-09
  • 打赏
  • 举报
回复
可以考虑用公平锁的方式来处理. 当然也可以考虑通过定义一个标志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;
	}
}
yjsl__ 2018-04-07
  • 打赏
  • 举报
回复
这根本不是多线程问题,用两个list记录内容,合并排序

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧