本文共 7359 字,大约阅读时间需要 24 分钟。
new Thread(new Runnable() { @Override public void run() { while(true){ System.out.println("just a test " + Thread.currentThread().getName()+ " "+new Date()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start();输出结果:just a test Thread-0 Sun Sep 17 09:48:48 CST 2017just a test Thread-0 Sun Sep 17 09:48:49 CST 2017just a test Thread-0 Sun Sep 17 09:48:50 CST 2017just a test Thread-0 Sun Sep 17 09:48:51 CST 2017just a test Thread-0 Sun Sep 17 09:48:52 CST 2017just a test Thread-0 Sun Sep 17 09:48:53 CST 2017
public class CreatThread02 implements Runnable{ @Override public void run() { System.out.println("currentThread : "+ Thread.currentThread().getName()+ " say hello for you ... time --> " + new Date()); } public static void main(String[] args) { Runnable thread01 = new CreatThread02(); Runnable thread02 = new CreatThread02(); while (true){ try { thread01.run(); Thread.sleep(500); thread02.run(); Thread.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); } } }}输出结果:currentThread : main say hello for you ... time --> Sun Sep 17 10:02:45 CST 2017currentThread : main say hello for you ... time --> Sun Sep 17 10:02:45 CST 2017currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017currentThread : main say hello for you ... time --> Sun Sep 17 10:02:47 CST 2017
public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(2); for(int i = 0 ; i< 100 ;i++){ RunClass run = new RunClass(i); service.execute(run); } service.shutdown(); } static class RunClass implements Runnable{ private int index ; public RunClass(int index ){ this.index = index; } @Override public void run() { long sleeptime = (long)(Math.random()*1000); System.out.println("the RunClassNum--> "+index +" cunrrentThread --> "+ Thread.currentThread().getName()+ " come back over ..." + " Sleep Time -->" +sleeptime); try { Thread.sleep(sleeptime); } catch (InterruptedException e) { e.printStackTrace(); } } }输出结果:the RunClassNum--> 87 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->152the RunClassNum--> 88 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->207the RunClassNum--> 89 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->958the RunClassNum--> 90 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->484the RunClassNum--> 91 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->767the RunClassNum--> 92 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->202the RunClassNum--> 93 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->666the RunClassNum--> 94 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->454
public class Controller4Thread01 extends Thread { // 1.使用sleep使当前线程暂停一段时间// 2.使用join是当前线程加入另一个线程 public static int result; public Controller4Thread01(String name ){ super(name); } @Override public void run() { result = (int)( Math.random()*1000); System.out.println("currentThread name-->"+ this.getName() + " get result -->" + result); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String[] args) { Controller4Thread01 thread01 = new Controller4Thread01("测试线程1"); thread01.start(); long startTime = System.currentTimeMillis(); System.out.println("开始时间为--> " + startTime); System.out.println("thread 未加入之前 .. result-->"+result); try { thread01.join(); } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); System.out.println("结束时间为--> "+ endTime + " 时间间隔-->" + (endTime-startTime)); }}输出结果:
public class Controller4Thread01 extends Thread { // 1.使用sleep使当前线程暂停一段时间// 2.使用join是当前线程加入另一个线程// 3.使用interrupt打断线程 public static int result; public long time ; public Controller4Thread01(String name ){ super(name); } @Override public void run() { long starttime = System.currentTimeMillis(); try { result = (int)( Math.random()*1000); System.out.println("currentThread name-->"+ this.getName() + " get result -->" + result); Thread.sleep(4000); long endTime = System.currentTimeMillis(); time = endTime-starttime; System.out.println(this.getName() + "--> 线程正常运行,且当前线程已运行时间 --> "+ time + "ms"); } catch (InterruptedException e) { long endTime = System.currentTimeMillis(); time = endTime-starttime; System.out.println(this.getName() + "--> 线程被中断,且当前线程已运行时间 --> "+ time + "ms"); e.printStackTrace(); } } public static void main(String[] args) { Controller4Thread01 thread01 = new Controller4Thread01("测试线程1"); thread01.start(); long startTime = System.currentTimeMillis(); System.out.println("开始时间为--> " + startTime); System.out.println("thread 未加入之前 .. result-->"+result); try { thread01.join(2000); long endTime = System.currentTimeMillis(); thread01.interrupt(); System.out.println("结束时间为--> "+ endTime + " 时间间隔-->" + (endTime-startTime)); } catch (InterruptedException e) { e.printStackTrace(); } }}//输出结果:开始时间为--> 1505642692997thread 未加入之前 .. result-->0currentThread name-->测试线程1 get result -->648结束时间为--> 1505642694998 时间间隔-->2001java.lang.InterruptedException: sleep interrupted测试线程1--> 线程被中断,且当前线程已运行时间 --> 2000ms at java.lang.Thread.sleep(Native Method) at Thread.threadConcurrency01.Part02.Controller4Thread01.run(Controller4Thread01.java:22)
(群号:51920822) –>