线程练习题1

有四个线程1、2、3、4。线程1的功能就是输出A,线程2的功能就是输出B,以此类推......... 现在有四个文件file1,file2,file3,
file4。初始都为空。
现要让四个文件呈如下格式:
file1:A B C D A B....
file2:B C D A B C....
file3:C D A B C D....

file4:D A B C D A.... 


package com.thread.first;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 
 * @author Will
 * 
 */
public class ThreadFirst {

	private static Lock lock = new ReentrantLock();
	private static Condition condition = null;

	public static void main(String[] args) {
		condition = lock.newCondition();

		FileUtil util = new FileUtil(4);

		ExecutorService executor = Executors.newCachedThreadPool();
		executor.execute(new Print(1, "A", util));
		executor.execute(new Print(2, "B", util));
		executor.execute(new Print(3, "C", util));
		executor.execute(new Print(4, "D", util));

		executor.shutdown();
	}

	static class FileUtil {
		private int size;
		public FileUtil(int size){
			this.size=size;
		}
		
		private int currentThreadNum = 1;
		private int currentFileNum = 1;

		public void write(int num, String str) {
			System.out.println(str+" is written into file"+currentFileNum);
			currentThreadNum = num;
			generateNextThreadNum();
			generateNextFileNum();
			
		}

		public int getCurrentThreadNum() {
			return currentThreadNum;
		}

		/**
		 * 保证文件一个接着一个走1,2,3,4,1,2,....
		 */
		private void generateNextFileNum() {
			int tmp;
			tmp= currentFileNum+1;
			tmp = tmp%size;
			if(tmp==0){
				tmp=size;
			}
			currentFileNum=tmp;
		}

		private void generateNextThreadNum() {
			//文件执行一圈,+2开始 并且考虑大于size重归0
			if(currentFileNum == size){
				if (currentThreadNum < (size-1)) {
					currentThreadNum += 2;
				} else {
					currentThreadNum = (currentThreadNum + 2) % size;
				}
			}else{
				if (currentThreadNum == size) {
					currentThreadNum = 1;
				} else {
					currentThreadNum++;
				}
			}
		}
	}

	static class Print implements Runnable {
		private int threadNum;
		private String str;
		private FileUtil util;

		public Print(int threadNum, String str, FileUtil util) {
			this.threadNum = threadNum;
			this.str = str;
			this.util = util;
		}

		public void run() {
			int flat = 16;
			while (flat-- > 0) {
				lock.lock();
				try {
					while (threadNum != util.getCurrentThreadNum()) {
						condition.await();
					}
					util.write(threadNum, str);
					condition.signalAll();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}
		}
	}
}

结果

A is written into file1
B is written into file2
C is written into file3
D is written into file4
B is written into file1
C is written into file2
D is written into file3
A is written into file4
C is written into file1
D is written into file2
A is written into file3
B is written into file4
D is written into file1
A is written into file2
B is written into file3
C is written into file4
A is written into file1
B is written into file2
C is written into file3
D is written into file4
B is written into file1
C is written into file2
D is written into file3
A is written into file4
C is written into file1
D is written into file2
A is written into file3
B is written into file4
D is written into file1
A is written into file2
B is written into file3
C is written into file4
A is written into file1
B is written into file2
C is written into file3
D is written into file4
B is written into file1
C is written into file2
D is written into file3
A is written into file4
C is written into file1
D is written into file2
A is written into file3
B is written into file4
D is written into file1
A is written into file2
B is written into file3
C is written into file4
A is written into file1
B is written into file2
C is written into file3
D is written into file4
B is written into file1
C is written into file2
D is written into file3
A is written into file4
C is written into file1
D is written into file2
A is written into file3
B is written into file4
D is written into file1
A is written into file2
B is written into file3
C is written into file4




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值