运行List A中的程序,运行结果见List B
List A :扩展线程类
class TestThreads { public static void main (String args []) { class MyThread extends Thread { String which; MyThread (String which) { this.which = which; } public void run() { int iterations = (int)(Math.random()*100) %15; int sleepinterval = (int)(Math.random()*1000); System.out.println(which + " running for " + iterations +" iterations"); System.out.println(which + " sleeping for " + sleepinterval + "ms between loops"); for (int i = 0; < iterations; i++) { System.out.println(which +" " + i); try { Thread.sleep(sleepinterval); } catch (InterruptedException e) {} } } } MyThread a = new MyThread("Thread A"); MyThread b = new MyThread("Thread B"); MyThread c = new MyThread("Thread C"); a.start(); b.start(); c.start(); } } |
延伸阅读
文章来源于领测软件测试网 https://www.ltesting.net/