两个部门面了我
第一个部门是写java的但是我表现的很差
这个部门是写汽车的模拟仿真的主要是C++和Python,设计算法和计算机视觉,没有问java和数据库,问题都比较有意思
重构优化版本:
public class ThreadOrderRun3 {File fileA = new File(");File fileB = new File(");File fileC = new File(");File fileD = new File(");FileWriter fwA;FileWriter fwB;FileWriter fwC;FileWriter fwD;public ThreadOrderRun3() throws IOException {//初始化创建文件A B C Dif (ists() || ists() || ists() || ists()) {fileA.delete();fileB.delete();fileC.delete();fileD.delete();}ateNewFile();ateNewFile();ateNewFile();ateNewFile();fwA = new FileWriter(fileA);fwB = new FileWriter(fileB);fwC = new FileWriter(fileC);fwD = new FileWriter(fileD);}private void firstRun(String str) throws IOException, InterruptedException {while (true) {
// try {
// lock1.lock();
// if (lock1id != 1) {
// condition11.await();
// }
// fileWriter("fileA", "1 ");
// lock1id = 2;
// condition12.signal();
// } catch (IOException e) {
// e.printStackTrace();
// } finally {
// lock1.unlock();
// }synchronized (fileA) {fileWriter("fileA", str);}Thread.sleep(50);synchronized (fileD) {fileWriter("fileD", str);}Thread.sleep(50);synchronized (fileC) {fileWriter("fileC", str);}Thread.sleep(50);synchronized (fileB) {fileWriter("fileB", str);}Thread.sleep(50);}}private void secondRun(String str) throws IOException, InterruptedException {while (true) {synchronized (fileB) {fileWriter("fileB", str);}Thread.sleep(50);synchronized (fileA) {fileWriter("fileA", str);}Thread.sleep(50);synchronized (fileD) {fileWriter("fileD", str);}Thread.sleep(50);synchronized (fileC) {fileWriter("fileC", str);}Thread.sleep(50);}}private void thirdRun(String str) throws IOException, InterruptedException {while (true) {synchronized (fileC) {fileWriter("fileC", str);}Thread.sleep(50);synchronized (fileB) {fileWriter("fileB", str);}Thread.sleep(50);synchronized (fileA) {fileWriter("fileA", str);}Thread.sleep(50);synchronized (fileD) {fileWriter("fileD", str);}Thread.sleep(50);}}private void fourthRun(String str) throws IOException, InterruptedException {while (true) {synchronized (fileD) {fileWriter("fileD", str);}Thread.sleep(50);synchronized (fileC) {fileWriter("fileC", str);}Thread.sleep(50);synchronized (fileB) {fileWriter("fileB", str);}Thread.sleep(50);synchronized (fileA) {fileWriter("fileA", str);}Thread.sleep(50);}}private void fileWriter(String fileName, String str) throws IOException {if (fileName == null) {return;}if (fileName.equals("fileA")) {fwA.write(str);fwA.flush();}if (fileName.equals("fileB")) {fwB.write(str);fwB.flush();}if (fileName.equals("fileC")) {fwC.write(str);fwC.flush();}if (fileName.equals("fileD")) {fwD.write(str);fwD.flush();}}public static void main(String[] args) throws IOException {ThreadOrderRun3 threadOrderRun3 = new ThreadOrderRun3();new Thread(new Runnable() {@Overridepublic void run() {try {threadOrderRun3.firstRun("1 ");} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {threadOrderRun3.secondRun("2 ");} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {threadOrderRun3.thirdRun("3 ");} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}).start();new Thread(new Runnable() {@Overridepublic void run() {try {threadOrderRun3.fourthRun("4 ");} catch (IOException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}}}).start();}
}
本文发布于:2024-02-04 10:45:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170705373654872.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |