main gogogo 5 Thread-1 run 10
问题:join()
问题6:
Question123:
public class Ques123 { private static Object resource = new Object(); private static void delay(long n){ try { Thread.sleep(n); } catch (Exception e) { System.out.println("error"); e.printStackTrace(); } } public static void main(String[] args) { System.out.println("StartMain"); new Thread1().start(); delay(1000); Thread2 thread2 = new Thread2(); thread2.start(); delay(1000);
thread2.interrupt(); delay(1000); System.out.println("EndMain"); } static class Thread1 extends Thread{ public void run(){ synchronized (resource) { System.out.println("Start1"); delay(6000); System.out.println("End1"); } } } static class Thread2 extends Thread{ public void run(){ synchronized (resource) { System.out.println("Start2"); delay(2000); System.out.println("End2"); } } } }
输出结果:
StartMain Start1 EndMain End1
: sleep interruptedStart2 error End2
at java.lang.Thread.sleep() at Ques123.delay() at Ques123.access$1() at Ques123$Thread2.run()
问题:interrupt();后为何报错
问题6:
Question 125:
public class Ques125 { int x = 0; public class Runner implements Runnable{ @Override public void run() { int current = 0; for(int i=0;i<4;i++){ current = x; System.out.println(current+","+Thread.currentThread().getName()); x = current + 2; } } } public static void main(String[] args) { new Ques125().go(); } public void go(){ Runnable runnable = new Runner(); new Thread(runnable).start(); new Thread(runnable).start(); } }
选题答案:0, 2, 4 ,4 ,6 ,8 ,10 ,6 和 0,2,4,6,8,10,12,14
自己试验的结果如下: 输出结果1:
0,Thread-0 0,Thread-1 2,Thread-0 2,Thread-1 4,Thread-0
4,Thread-1 6,Thread-1 6,Thread-0
输出结果2: 0,Thread-0 2,Thread-0 2,Thread-1 4,Thread-0 4,Thread-1 6,Thread-0 6,Thread-1 8,Thread-1
输出结果3: 0,Thread-0 2,Thread-0 4,Thread-0 6,Thread-0 8,Thread-1 10,Thread-1 12,Thread-1 14,Thread-1
问题7:
Question126:
public class Ques126 { privatenames = new public synchronized void add(String name){ } public synchronized void printAll(){ for(int i=0;i
public void run(){ sl.add("A "+Thread.currentThread().getName()); sl.add("B "+Thread.currentThread().getName()); sl.add("C "+Thread.currentThread().getName()); sl.printAll(); } }.start(); } } }
答案结果:
A B C A B C A B C A B C A A B C A B C
试验输出结果: 结果1:
A Thread-0 B Thread-0 C Thread-0 A Thread-0 B Thread-0 C Thread-0 A Thread-1 B Thread-1 C Thread-1 结果2: A Thread-0 A Thread-1 B Thread-0 B Thread-1 C Thread-0 C Thread-1 A Thread-0 A Thread-1 B Thread-0 B Thread-1 C Thread-0 C Thread-1
设计模式篇
问题1:傅
设计模式是一套被反复使用验证, 对代码设计经验的总结。使用设计模式是为了可重用代码、提高代码可维护性, 可理解性、保证代码可靠性。
一方面, 设计模式是对代码设计经验的总结, 另一方面, 设计模式也被经常的使用到各高效, 成熟, 稳定的系统架构中
也就是说在现有成熟系统中运行着无数这样的高质量的代码, 比如Java本身, Android的设计,