最近在面試阿里的,遇到一個(gè)筆試題献幔,題目是這樣的:
多線(xiàn)程寫(xiě)文件懂傀,有3個(gè)線(xiàn)程1、2蜡感、3蹬蚁。線(xiàn)程1的功能就是輸出t1,線(xiàn)程2的功能就是輸出t2郑兴,以此類(lèi)推.........犀斋,
現(xiàn)在有三個(gè)文件file1,file2,file3。初始都為空情连,現(xiàn)要讓三個(gè)文件呈如下格式:
file1:t1 t2 t3 t1 t2....
file2:t2 t3 t1 t2 t3....
file3:t3 t1 t2 t3 t1….
下面以2中方式完成這題
第一種采用ReentranLock+condition進(jìn)行實(shí)現(xiàn)叽粹,具體代碼如下:
package thread;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 多線(xiàn)程寫(xiě)文件,有3個(gè)線(xiàn)程1却舀、2虫几、3。線(xiàn)程1的功能就是輸出t1挽拔,線(xiàn)程2的功能就是輸出t2辆脸,以此類(lèi)推.........,
* 現(xiàn)在有三個(gè)文件file1,file2,file3螃诅。初始都為空啡氢,現(xiàn)要讓三個(gè)文件呈如下格式:
* 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)前線(xiàn)程輸出的文件
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();
}
}
/**
* 實(shí)際打印方法
* @param threadNumber 當(dāng)前線(xiàn)程id 對(duì)應(yīng)1术裸,2倘是,3
* @param current 控制當(dāng)前線(xiàn)程的condition
* @param next 控制后一個(gè)線(xiàn)程的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();
}
}
}
/**
* 寫(xiě)入文件
**/
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)前線(xiàn)程在第loop循環(huán)情況下寫(xiě)入哪個(gè)文件
**/
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進(jìn)行控制線(xiàn)程循環(huán)打印辨绊。
package thread;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 多線(xiàn)程寫(xiě)文件,有3個(gè)線(xiàn)程1匹表、2门坷、3宣鄙。線(xiàn)程1的功能就是輸出t1,線(xiàn)程2的功能就是輸出t2默蚌,以此類(lèi)推.........冻晤,
* 現(xiàn)在有三個(gè)文件file1,file2,file3。初始都為空绸吸,現(xiàn)要讓三個(gè)文件呈如下格式:
* 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;
}
}
}
}
/**
* 寫(xiě)入文件
**/
private void writeFile(FileWriter fw, String s) {
try {
fw.append(s);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判斷在當(dāng)前線(xiàn)程在第loop循環(huán)情況下寫(xiě)入哪個(gè)文件
**/
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;
}
}
}
}
兩種方法都能實(shí)現(xiàn)循環(huán)打印鼻弧,但是第二種方式?jīng)]有加鎖,對(duì)cpu的消耗較大锦茁,而且性能不高攘轩,執(zhí)行時(shí)間也耗時(shí)較長(zhǎng)。在打印100萬(wàn)的數(shù)據(jù)中码俩,第一種方法耗時(shí) 8431ms度帮, 第二種方法耗時(shí) 14406ms。所以面試時(shí)建議寫(xiě)第一種稿存,第二種僅作參考笨篷。