有四个线程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。初始都为空。
现要让四个文件呈如下格式:
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