标签:class ram ace 基础 lis current out this ext
1.简介
java基础知识部分线程创建的三种方式、线程执行的样例。
代码地址:http://git.oschina.net/blue_phantom/javaj
包位置:package com.bluej.javaj.thread.first;
2.创建线程
继承Thread类、实现Runnale接口、实现Callable接口。
1 /** 2 * 创建线程方式一 3 * @author xingdongjie 4 * 5 */ 6 class CreateThread1 extends Thread { 7 @Override 8 public void run() { 9 threadHandle(); 10 } 11 12 private void threadHandle(){ 13 System.out.println("---通过继承Thread:do what you want---"); 14 } 15 } 16 /** 17 * 创建线程方式二 18 * @author xingdongjie 19 * 20 */ 21 class CreateThread2 implements Runnable { 22 @Override 23 public void run() { 24 threadHandle(); 25 } 26 private void threadHandle(){ 27 System.out.println("---通过实现Runnable:do what you want---"); 28 } 29 } 30 /** 31 * 线程创建方式三 32 * @author xingdongjie 33 * 34 */ 35 class CreateThread3 implements Callable<CallableResult>{ 36 private CallableResult result; 37 38 @Override 39 public CallableResult call() throws Exception { 40 result = new CallableResult(); 41 result.setStart(System.currentTimeMillis()); 42 43 threadHandle(); 44 45 result.setEnd(System.currentTimeMillis()); 46 result.setUserTime(result.getEnd()-result.getStart()); 47 result.setMessage("--执行成功--"); 48 49 return result; 50 } 51 52 private void threadHandle(){ 53 try { 54 Thread.sleep(new Random().nextInt(10) * 1000); 55 } catch (Exception e) { 56 } 57 System.out.println("---通过实现Callable:do what you want---"); 58 } 59 }
第三种方式实现Callable接口,可以返回执行结果,所以这里创建了一个CallableResult类用做执行结果类。
1 /** 2 * callable方式线程执行结果类 3 * @author xingdongjie 4 * 5 */ 6 class CallableResult implements Serializable{ 7 private static final long serialVersionUID = 1L; 8 9 private Long start; 10 private Long end; 11 private Long userTime; 12 private String message; 13 14 public Long getStart() { 15 return start; 16 } 17 public void setStart(Long start) { 18 this.start = start; 19 } 20 public Long getEnd() { 21 return end; 22 } 23 public void setEnd(Long end) { 24 this.end = end; 25 } 26 public String getMessage() { 27 return message; 28 } 29 public void setMessage(String message) { 30 this.message = message; 31 } 32 33 public Long getUserTime() { 34 return userTime; 35 } 36 public void setUserTime(Long userTime) { 37 this.userTime = userTime; 38 } 39 @Override 40 public String toString() { 41 return "CallableResult [start=" + start + ", end=" + end 42 + ", userTime=" + userTime + ", message=" + message + "]"; 43 }
3.执行线程
创建完线程后,启动线程。方式一Thread、方式二Runnale创建的线程后执行方式相同,但是方式三Callable创建的线程执行有些不同。
1 /** 2 * 执行线程方式一 3 */ 4 public static void doTread1(){ 5 CreateThread1 thread1 = new CreateThread1(); 6 new Thread(thread1).start(); 7 } 8 9 /** 10 * 执行线程方式二 11 */ 12 public static void doThread2(){ 13 CreateThread2 thread2 = new CreateThread2(); 14 new Thread(thread2).start(); 15 } 16 17 /** 18 * 执行线程方式三 19 */ 20 public static void doThread3(){ 21 CreateThread3 thread3 = new CreateThread3(); 22 CallableResult result=null; 23 //可以通过2种方式执行Callable线程 24 result =chooseOne(thread3); 25 //result = chooseTwo(thread3); 26 System.out.println("---线程执行结果result:"+result); 27 28 29 }
Callable线程的执行在这里有两种不同的执行方式,所以用了两个方法,使用时可以二选一。
1 /** 2 * 通过FutureTask方式执行线程 3 * @param thread3 4 * @return 5 */ 6 public static CallableResult chooseOne(CreateThread3 thread3){ 7 CallableResult result=null; 8 FutureTask<CallableResult> future = new FutureTask<CallableResult>(thread3); 9 new Thread(future).start(); 10 try { 11 result = future.get(); 12 } catch (Exception e) { 13 e.printStackTrace(); 14 } 15 return result; 16 } 17 /** 18 * 通过线程池方式执行线程 19 * @param thread3 20 * @return 21 */ 22 public static CallableResult chooseTwo(CreateThread3 thread3){ 23 CallableResult result=null; 24 //创建一种简单的线程池 25 ExecutorService executorService = Executors.newCachedThreadPool(); 26 Future<CallableResult> future = executorService.submit(thread3); 27 try { 28 result = future.get(); 29 } catch (Exception e) { 30 e.printStackTrace(); 31 } 32 return result; 33 }
4.小结
以上是线程创建及执行的不同方式。
1 package com.bluej.javaj.thread.first; 2 3 import java.io.Serializable; 4 import java.util.Random; 5 import java.util.concurrent.Callable; 6 import java.util.concurrent.ExecutorService; 7 import java.util.concurrent.Executors; 8 import java.util.concurrent.Future; 9 import java.util.concurrent.FutureTask; 10 11 /** 12 * 线程的创建及执行 13 * @author xingdongjie 14 * 15 */ 16 public class CreateAndDoThread { 17 18 /** 19 * @param args 20 */ 21 public static void main(String[] args) { 22 //3种创建线程的执行demo 23 //doTread1(); 24 //doThread2(); 25 doThread3(); 26 27 } 28 29 /** 30 * 执行线程方式一 31 */ 32 public static void doTread1(){ 33 CreateThread1 thread1 = new CreateThread1(); 34 new Thread(thread1).start(); 35 } 36 37 /** 38 * 执行线程方式二 39 */ 40 public static void doThread2(){ 41 CreateThread2 thread2 = new CreateThread2(); 42 new Thread(thread2).start(); 43 } 44 45 /** 46 * 执行线程方式三 47 */ 48 public static void doThread3(){ 49 CreateThread3 thread3 = new CreateThread3(); 50 CallableResult result=null; 51 //可以通过2种方式执行Callable线程 52 result =chooseOne(thread3); 53 //result = chooseTwo(thread3); 54 System.out.println("---线程执行结果result:"+result); 55 56 57 } 58 /** 59 * 通过FutureTask方式执行线程 60 * @param thread3 61 * @return 62 */ 63 public static CallableResult chooseOne(CreateThread3 thread3){ 64 CallableResult result=null; 65 FutureTask<CallableResult> future = new FutureTask<CallableResult>(thread3); 66 new Thread(future).start(); 67 try { 68 result = future.get(); 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } 72 return result; 73 } 74 /** 75 * 通过线程池方式执行线程 76 * @param thread3 77 * @return 78 */ 79 public static CallableResult chooseTwo(CreateThread3 thread3){ 80 CallableResult result=null; 81 //创建一种简单的线程池 82 ExecutorService executorService = Executors.newCachedThreadPool(); 83 Future<CallableResult> future = executorService.submit(thread3); 84 try { 85 result = future.get(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 return result; 90 } 91 } 92 93 94 /** 95 * 创建线程方式一 96 * @author xingdongjie 97 * 98 */ 99 class CreateThread1 extends Thread { 100 @Override 101 public void run() { 102 threadHandle(); 103 } 104 105 private void threadHandle(){ 106 System.out.println("---通过继承Thread:do what you want---"); 107 } 108 } 109 /** 110 * 创建线程方式二 111 * @author xingdongjie 112 * 113 */ 114 class CreateThread2 implements Runnable { 115 @Override 116 public void run() { 117 threadHandle(); 118 } 119 private void threadHandle(){ 120 System.out.println("---通过实现Runnable:do what you want---"); 121 } 122 } 123 /** 124 * 线程创建方式三 125 * @author xingdongjie 126 * 127 */ 128 class CreateThread3 implements Callable<CallableResult>{ 129 private CallableResult result; 130 131 @Override 132 public CallableResult call() throws Exception { 133 result = new CallableResult(); 134 result.setStart(System.currentTimeMillis()); 135 136 threadHandle(); 137 138 result.setEnd(System.currentTimeMillis()); 139 result.setUserTime(result.getEnd()-result.getStart()); 140 result.setMessage("--执行成功--"); 141 142 return result; 143 } 144 145 private void threadHandle(){ 146 try { 147 Thread.sleep(new Random().nextInt(10) * 1000); 148 } catch (Exception e) { 149 } 150 System.out.println("---通过实现Callable:do what you want---"); 151 } 152 } 153 /** 154 * callable方式线程执行结果类 155 * @author xingdongjie 156 * 157 */ 158 class CallableResult implements Serializable{ 159 private static final long serialVersionUID = 1L; 160 161 private Long start; 162 private Long end; 163 private Long userTime; 164 private String message; 165 166 public Long getStart() { 167 return start; 168 } 169 public void setStart(Long start) { 170 this.start = start; 171 } 172 public Long getEnd() { 173 return end; 174 } 175 public void setEnd(Long end) { 176 this.end = end; 177 } 178 public String getMessage() { 179 return message; 180 } 181 public void setMessage(String message) { 182 this.message = message; 183 } 184 185 public Long getUserTime() { 186 return userTime; 187 } 188 public void setUserTime(Long userTime) { 189 this.userTime = userTime; 190 } 191 @Override 192 public String toString() { 193 return "CallableResult [start=" + start + ", end=" + end 194 + ", userTime=" + userTime + ", message=" + message + "]"; 195 } 196 197 }
标签:class ram ace 基础 lis current out this ext
原文地址:http://www.cnblogs.com/bluej/p/6520666.html