最近在面試阿里的,遇到一個筆試題,題目是這樣的:
多線程寫文件,有3個線程1、2、3。線程1的功能就是輸出t1,線程2的功能就是輸出t2,以此類推.........,
現(xiàn)在有三個文件file1,file2,file3。初始都為空,現(xiàn)要讓三個文件呈如下格式:
file1:t1 t2 t3 t1 t2....
file2:t2 t3 t1 t2 t3....
file3:t3 t1 t2 t3 t1….
下面以2中方式完成這題
第一種采用ReentranLock+condition進行實現(xiàn),具體代碼如下:
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,以此類推.........,
* 現(xiàn)在有三個文件file1,file2,file3。初始都為空,現(xiàn)要讓三個文件呈如下格式:
* 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");
//當(dāng)前循環(huán)的圈數(shù),用于判斷當(dāng)前線程輸出的文件
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 當(dāng)前線程id 對應(yīng)1,2,3
* @param current 控制當(dāng)前線程的condition
* @param next 控制后一個線程的condition
* @param str 打印的內(nèi)容
* @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();
}
}
/**
* 判斷在當(dāng)前線程在第loop循環(huán)情況下寫入哪個文件
**/
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進行控制線程循環(huán)打印。
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,以此類推.........,
* 現(xiàn)在有三個文件file1,file2,file3。初始都為空,現(xiàn)要讓三個文件呈如下格式:
* 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();
}
}
/**
* 判斷在當(dāng)前線程在第loop循環(huán)情況下寫入哪個文件
**/
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;
}
}
}
}
兩種方法都能實現(xiàn)循環(huán)打印,但是第二種方式?jīng)]有加鎖,對cpu的消耗較大,而且性能不高,執(zhí)行時間也耗時較長。在打印100萬的數(shù)據(jù)中,第一種方法耗時 8431ms, 第二種方法耗時 14406ms。所以面試時建議寫第一種,第二種僅作參考。