一道关于笔试的多线程题目

四个线程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();

	}
}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值