抢分啦----线程问题(在线等)
以下程序中有两个“断点”
要求:只有当到达list_3的中断点时,list_1的断点后的程序才执行
我不知道怎么设置一个标志来判断list_3中的前面部分程序已经执行
希望大家写出完整的程序,不要用文字来讲道理,谢谢
也可以按照我的思路把重新做一个程序或者修改我的程序
真实要求是:
有四个进程
list_1list_2list_3
要求同时三个进程同时开始
list_1中有方法
list_1_1(打印“list_1_1 需要1秒”)
list_1_2(打印“list_1_2 需要1秒”)(步骤2)
list_1_3(打印“list_1_3 需要1秒”)
执行顺序为:1 2 3
list_2中有方法
list_2_1(打印“list_2_1 需要2秒”)
list_2_2(打印“list_2_2 需要2秒”)
list_2_3(打印“list_2_3 需要2秒”)
执行顺序为:1 2 3
list_3中有方法
list_3_1(打印“list_3_1 需要3秒”)(步骤1)
list_3_2(打印“list_3_2 需要3秒”)
list_3_3(打印“list_3_3 需要3秒”)
执行顺序为:1 2 3
额外要求:只有当(步骤1)完成后(步骤2)才会开始
class TestThread { public static void main(String[] args) { Thread list_1=new list_1(); Thread list_2=new list_2(); Thread list_3=new list_3(); list_1.start(); list_2.start(); list_3.start(); }}class list_1 extends Thread { list_1() { } public void run() { System.out.println("list_1_1 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } //断点 //需要判断list_3中的(步骤A)有没有完成,如果完成了,就执行以下的程序,如果没有就等到完成后再执行 System.out.println("list_1_2 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } System.out.println("list_1_3 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } }}class list_2 extends Thread { list_2() { } int i=0; public void run(){ System.out.println("list_2_1 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } System.out.println("list_2_2 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } System.out.println("list_2_3 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } }}class list_3 extends Thread { list_3() { } public void run() { System.out.println("list_3_1 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } //断点 //以上就是(步骤A)如果完成到这里,list_1类的步骤才开始进行 System.out.println("list_3_2 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } System.out.println("list_3_3 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } }}
class Test{ public static void main(String[] args) { Thread list_1=new list_1(); Thread list_2=new list_2(); Thread list_3=new list_3(); list_1.start(); list_2.start(); list_3.start(); }}class list_1 extends Thread { list_1() { } public void run() { System.out.println("list_1_1 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } while(!list_3.key){ try { sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //断点 //需要判断list_3中的(步骤A)有没有完成,如果完成了,就执行以下的程序,如果没有就等到完成后再执行 System.out.println("list_1_2 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } System.out.println("list_1_3 需要1秒"); try { sleep(1000);// 挂起1000 } catch (InterruptedException e) { return; } }}class list_2 extends Thread { list_2() { } int i=0; public void run(){ System.out.println("list_2_1 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } System.out.println("list_2_2 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } System.out.println("list_2_3 需要2秒"); try { sleep(2000);// 挂起2000 } catch (InterruptedException e) { return; } }}class list_3 extends Thread { public static boolean key = false; list_3() { } public void run() { System.out.println("list_3_1 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } //断点 //以上就是(步骤A)如果完成到这里,list_1类的步骤才开始进行 key = true; System.out.println("list_3_2 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } System.out.println("list_3_3 需要3秒"); try { sleep(3000);// 挂起3000 } catch (InterruptedException e) { return; } }}