四個執行緒t1,t2,t3,t4,向4個檔案中寫入資料,t1只能寫入1,t2只能寫入2,t3只能寫入3,t4只能寫入4,對4個檔案A,B,C,D寫入如下內容:
A:123412341234.....
B:234123412341....
C:341234123412....
D:412341234123....
怎麼實現同步可以讓執行緒並行工作?
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class Hello
{
static boolean b=false;
static ReentrantLock lock=new ReentrantLock();
static ReentrantLock lock_2=new ReentrantLock();
static Condition condition_for=lock.newCondition();
//執行緒Thread_t1~Thread_t4分別只寫入1~4
class Thread_t1 extends Thread
{
String path;
Thread_t1(String path)
{
this.path=path;
}
public void run()
{
print(path,1);
}
}
class Thread_t2 extends Thread
{
String path;
Thread_t2(String path)
{
this.path=path;
}
public void run()
{
print(path,2);
}
}
class Thread_t3 extends Thread
{
String path;
Thread_t3(String path)
{
this.path=path;
}
public void run()
{
print(path,3);
}
}
class Thread_t4 extends Thread
{
String path;
Thread_t4(String path)
{
this.path=path;
}
public void run()
{
print(path,4);
}
}
//寫入方法
public void print(String path, int i)
{
lock_2.lock();
PrintWriter pw=null;
try {
pw=new PrintWriter(new FileWriter("c:/work/abcd/"+path+".txt",true),true);
pw.print(i);
} catch (Exception e) {
e.printStackTrace();
}finally{
pw.close();
lock_2.unlock();
}
}
//檔案類A~D
class File_A implements Runnable
{
int one,two,three,four;
File_A(int one,int two, int three, int four)
{
this.one=one;
this.two=two;
this.three=three;
this.four=four;
}
public void run()
{
for(int x=0;x<100;x++)
{
select("A",one,two,three,four);
}
}
}
class File_B implements Runnable
{
int one,two,three,four;
File_B(int one,int two, int three, int four)
{
this.one=one;
this.two=two;
this.three=three;
this.four=four;
}
public void run()
{
for(int x=0;x<100;x++)
{
select("B",one,two,three,four);
}
}
}
class File_C implements Runnable
{
int one,two,three,four;
File_C(int one,int two, int three, int four)
{
this.one=one;
this.two=two;
this.three=three;
this.four=four;
}
public void run()
{
for(int x=0;x<100;x++)
{
select("C",one,two,three,four);
}
}
}
class File_D implements Runnable
{
int one,two,three,four;
File_D(int one,int two, int three, int four)
{
this.one=one;
this.two=two;
this.three=three;
this.four=four;
}
public void run()
{
for(int x=0;x<100;x++)
{
select("D",one,two,three,four);
}
}
}
//選擇執行緒方法,當前只允許進入一個檔案執行緒
public void select(String path,int one,int two, int three, int four)
{
lock.lock();
try
{
while(b)
{
condition_for.await();
}
b=true;
int[] order={one,two,three,four};//將傳入的1234,換成陣列方便用for遍歷
for(int i:order)
{
switch(i){
case 1:
Thread_t1 t1= new Thread_t1(path);
t1.start();
t1.join(); //t1.join()是等執行緒t1執行完後再執行當前執行緒
break;
case 2:
Thread_t2 t2= new Thread_t2(path);
t2.start();
t2.join();
break;
case 3:
Thread_t3 t3= new Thread_t3(path);
t3.start();
t3.join();
break;
case 4:
Thread_t4 t4= new Thread_t4(path);
t4.start();
t4.join();
break;
}
}
b=false;
}catch(Exception e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
//主函式
public static void main(String args[])
{
HelloJava hj=new HelloJava();
new Thread(hj.new File_A(1,2,3,4)).start();
new Thread(hj.new File_B(2,3,4,1)).start();
new Thread(hj.new File_C(3,4,1,2)).start();
new Thread(hj.new File_D(4,1,2,3)).start();
}
}