java多线程问题 有2个文件,a.txt,b.txt,分别有10行内容,现要求一行一行读取a.txt,b.txt。将读取的内容按顺序写到到c.txt文件中。比如a.txt内容为:13579b.txt内容为:2468输入到c.txt的内容为:123456789求如何编写 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这根本不是多线程问题,用两个list记录内容,合并排序 可以考虑用公平锁的方式来处理.当然也可以考虑通过定义一个标志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; }} 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); } JTextPane 设置样式的问题 java按钮连续点击事件 x += x;都发生了什么? 怎样导入外观包才可以用 drawArc和fillArc有什么区别 #applet 大家用的多吗?据说 IE 将不再支持内嵌java vm? 为了考研,离开这里半年,现在偶又回来了! geyf(其实我也不会) 及各位大侠,请再帮忙指点一下迷津!!! 请教简单问题——怎样在框架中定位不同组件的位置呀? 谁能告诉我SUN公司的java SCJP对找工作有用吗? 求大佬解惑 问一个关于Swing的问题,不知道还有没有人在用!
当然也可以考虑通过定义一个标志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;
}
}
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);
}