最近在面试阿里的,遇到一个笔试题,题目是这样的:
多线程写文件,有3个线程1、2、3。线程1的功能就是输出t1,线程2的功能就是输出t2,以此类推.........,
现在有三个文件file1,file2,file3。初始都为空,现要让三个文件呈如下格式:
file1:t1 t2 t3 t1 t2....
file2:t2 t3 t1 t2 t3....
file3:t3 t1 t2 t3 t1….
下面以2中方式完成这题
第一种采用ReentranLock+condition进行实现,具体代码如下:
package thread;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 多线程写文件,有3个线程1、2、3。线程1的功能就是输出t1,线程2的功能就是输出t2,以此类推.........,
* 现在有三个文件file1,file2,file3。初始都为空,现要让三个文件呈如下格式:
* file1:t1 t2 t3 t1 t2....
* file2:t2 t3 t1 t2 t3....
* file3:t3 t1 t2 t3 t1….
*
* @Author: huangyichun
* @Date: 2021/3/14
*/
public class CircleFileWriter {
public CircleFileWriter() throws IOException {
}
public static void main(String[] args) throws InterruptedException, IOException {
CircleFileWriter fileWriter = new CircleFileWriter();
Thread thread1 = new Thread(fileWriter::printT1);
Thread thread2 = new Thread(fileWriter::printT2);
Thread thread3 = new Thread(fileWriter::printT3);
thread1.start();
Thread.sleep(100);
thread2.start();
Thread.sleep(100);
thread3.start();
}
FileWriter fileWriter1 = new FileWriter("file1");
FileWriter fileWriter2 = new FileWriter("file2");
FileWriter fileWriter3 = new FileWriter("file3");
//当前循环的圈数,用于判断当前线程输出的文件
private volatile int loop = 0;
private final ReentrantLock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
private final int times = 100;
public void printT1() {
lock.lock();
try {
print(1, condition1, condition2, "t1");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printT2() {
lock.lock();
try {
print(2, condition2, condition3, "t2");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printT3() {
lock.lock();
try {
print(3, condition3, condition1, "t3");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* 实际打印方法
* @param threadNumber 当前线程id 对应1,2,3
* @param current 控制当前线程的condition
* @param next 控制后一个线程的condition
* @param str 打印的内容
* @throws InterruptedException
*/
private void print(int threadNumber, Condition current, Condition next, String str) throws InterruptedException {
for (int i = 0; i < 100; i++) {//打印100次
writeFile(getFile(threadNumber, loop), str);
if (threadNumber == 3) {
loop++;
}
next.signal();
if (i < times - 1) { //最后一次不需要等待
current.await();
}
}
}
/**
* 写入文件
**/
private void writeFile(FileWriter fw, String s) {
System.out.println(Thread.currentThread().getName() + "打印:" + s + "到");
try {
fw.append(s);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断在当前线程在第loop循环情况下写入哪个文件
**/
private FileWriter getFile(int threadNumber, int loop) {
if (threadNumber == 1) {
if (loop % 3 == 0) {
return fileWriter1;
} else if (loop % 3 == 1) {
return fileWriter3;
} else {
return fileWriter2;
}
} else if (threadNumber == 2) {
if (loop % 3 == 0) {
return fileWriter2;
} else if (loop % 3 == 1) {
return fileWriter1;
} else {
return fileWriter3;
}
} else {
if (loop % 3 == 0) {
return fileWriter3;
} else if (loop % 3 == 1) {
return fileWriter2;
} else {
return fileWriter1;
}
}
}
}
第二种方法,不是用锁,使用volatile进行控制线程循环打印。
package thread;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 多线程写文件,有3个线程1、2、3。线程1的功能就是输出t1,线程2的功能就是输出t2,以此类推.........,
* 现在有三个文件file1,file2,file3。初始都为空,现要让三个文件呈如下格式:
* file1:t1 t2 t3 t1 t2....
* file2:t2 t3 t1 t2 t3....
* file3:t3 t1 t2 t3 t1….
*
* @Author: huangyichun
* @Date: 2021/3/14
*/
public class CircleFileWriter2 {
public CircleFileWriter2() throws IOException {
}
public static void main(String[] args) throws InterruptedException, IOException {
CircleFileWriter2 fileWriter = new CircleFileWriter2();
Thread thread1 = new Thread(fileWriter::printT1);
Thread thread2 = new Thread(fileWriter::printT2);
Thread thread3 = new Thread(fileWriter::printT3);
thread1.start();
Thread.sleep(100);
thread2.start();
Thread.sleep(100);
thread3.start();
}
FileWriter fileWriter1 = new FileWriter("file1");
FileWriter fileWriter2 = new FileWriter("file2");
FileWriter fileWriter3 = new FileWriter("file3");
private volatile int loop = 0;
private volatile int state = 0;
public void printT1() {
print(1, "t1");
}
public void printT2() {
print(2, "t2");
}
public void printT3() {
print(3, "t3");
}
private void print(int threadNumber, String str) {
for (int i = 0; i < 100; i++) {//打印100次
while (true) {
if ((state % 3) + 1 == threadNumber) {
writeFile(getFile(threadNumber, loop), str);
if (threadNumber == 2) {
loop++;
}
state ++;
break;
}
}
}
}
/**
* 写入文件
**/
private void writeFile(FileWriter fw, String s) {
try {
fw.append(s);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断在当前线程在第loop循环情况下写入哪个文件
**/
private FileWriter getFile(int threadNumber, int loop) {
if (threadNumber == 1) {
if (loop % 3 == 0) {
return fileWriter1;
} else if (loop % 3 == 1) {
return fileWriter3;
} else {
return fileWriter2;
}
} else if (threadNumber == 2) {
if (loop % 3 == 0) {
return fileWriter2;
} else if (loop % 3 == 1) {
return fileWriter1;
} else {
return fileWriter3;
}
} else {
if (loop % 3 == 0) {
return fileWriter3;
} else if (loop % 3 == 1) {
return fileWriter2;
} else {
return fileWriter1;
}
}
}
}
两种方法都能实现循环打印,但是第二种方式没有加锁,对cpu的消耗较大,而且性能不高,执行时间也耗时较长。在打印100万的数据中,第一种方法耗时 8431ms, 第二种方法耗时 14406ms。所以面试时建议写第一种,第二种仅作参考。