创建多线程有几种方法 java创建线程的方式有几种?
\u591a\u7ebf\u7a0b\u5b9e\u73b0\u7684\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u6709\u4e09\u79cd\uff1a
(1)\u7ee7\u627fThread\u7c7b\uff0c\u91cd\u5199run\u51fd\u6570
\u521b\u5efa\uff1a
class xx extends Thread{
public void run(){
Thread.sleep(1000)//\u7ebf\u7a0b\u4f11\u77201000\u6beb\u79d2\uff0csleep\u4f7f\u7ebf\u7a0b\u8fdb\u5165Block\u72b6\u6001\uff0c\u5e76\u91ca\u653e\u8d44\u6e90
}}
\u5f00\u542f\u7ebf\u7a0b\uff1a
\u5bf9\u8c61.start()//\u542f\u52a8\u7ebf\u7a0b\uff0crun\u51fd\u6570\u8fd0\u884c
(2)\u5b9e\u73b0Runnable\u63a5\u53e3\uff0c\u91cd\u5199run\u51fd\u6570
\u5f00\u542f\u7ebf\u7a0b\uff1a
Thread t = new Thread(\u5bf9\u8c61)//\u521b\u5efa\u7ebf\u7a0b\u5bf9\u8c61
t.start()
(3)\u5b9e\u73b0Callable\u63a5\u53e3\uff0c\u91cd\u5199call\u51fd\u6570
Callable\u662f\u7c7b\u4f3c\u4e8eRunnable\u7684\u63a5\u53e3\uff0c\u5b9e\u73b0Callable\u63a5\u53e3\u7684\u7c7b\u548c\u5b9e\u73b0Runnable\u7684\u7c7b\u90fd\u662f\u53ef\u88ab\u5176\u5b83\u7ebf\u7a0b\u6267\u884c\u7684\u4efb\u52a1\u3002
Callable\u548cRunnable\u6709\u51e0\u70b9\u4e0d\u540c:
\u2460Callable\u89c4\u5b9a\u7684\u65b9\u6cd5\u662fcall()\uff0c\u800cRunnable\u89c4\u5b9a\u7684\u65b9\u6cd5\u662frun().
\u2461Callable\u7684\u4efb\u52a1\u6267\u884c\u540e\u53ef\u8fd4\u56de\u503c\uff0c\u800cRunnable\u7684\u4efb\u52a1\u662f\u4e0d\u80fd\u8fd4\u56de\u503c\u7684
\u2462call()\u65b9\u6cd5\u53ef\u629b\u51fa\u5f02\u5e38\uff0c\u800crun()\u65b9\u6cd5\u662f\u4e0d\u80fd\u629b\u51fa\u5f02\u5e38\u7684\u3002
\u2463\u8fd0\u884cCallable\u4efb\u52a1\u53ef\u62ff\u5230\u4e00\u4e2aFuture\u5bf9\u8c61\uff0cFuture\u8868\u793a\u5f02\u6b65\u8ba1\u7b97\u7684\u7ed3\u679c\u3002\u5b83\u63d0\u4f9b\u4e86\u68c0\u67e5\u8ba1\u7b97\u662f\u5426\u5b8c\u6210\u7684\u65b9\u6cd5,\u4ee5\u7b49
\u5f85\u8ba1\u7b97\u7684\u5b8c\u6210,\u5e76\u68c0\u7d22\u8ba1\u7b97\u7684\u7ed3\u679c.\u901a\u8fc7Future\u5bf9\u8c61\u53ef\u4e86\u89e3\u4efb\u52a1\u6267\u884c\u60c5\u51b5,\u53ef\u53d6\u6d88\u4efb\u52a1\u7684\u6267\u884c,\u8fd8\u53ef\u83b7\u53d6\u4efb\u52a1\u6267\u884c\u7684\u7ed3\u679c
java\u521b\u5efa\u7ebf\u7a0b\u7684\u65b9\u5f0f\u6709\u4e09\u79cd
\u7b2c\u4e00\u79cd\u662f\u7ee7\u627fThread\u7c7b \u5b9e\u73b0\u65b9\u6cd5run() \u4e0d\u53ef\u4ee5\u629b\u5f02\u5e38 \u65e0\u8fd4\u56de\u503c
\u7b2c\u4e8c\u79cd\u662f\u5b9e\u73b0Runnable\u63a5\u53e3 \u5b9e\u73b0\u65b9\u6cd5run() \u4e0d\u53ef\u4ee5\u629b\u5f02\u5e38 \u65e0\u8fd4\u56de\u503c
\u7b2c\u4e09\u79cd\u662f\u5b9e\u73b0Callable\u63a5\u53e3,\u63a5\u53e3\u4e2d\u8981\u8986\u76d6\u7684\u65b9\u6cd5\u662f public call() \u6ce8\u610f:\u6b64\u65b9\u6cd5\u53ef\u4ee5\u629b\u5f02\u5e38,\u800c\u524d\u4e24\u79cd\u4e0d\u80fd \u800c\u4e14\u6b64\u65b9\u6cd5\u53ef\u4ee5\u6709\u8fd4\u56de\u503c
\u7b2c\u4e09\u79cd\u5982\u4f55\u8fd0\u884c\u5462 Callable\u63a5\u53e3\u5728util.concurrent\u5305\u4e2d,\u7531\u7ebf\u7a0b\u6c60\u63d0\u4ea4
import java.util.concurrent.*;
ExecutorService e = Executors.newFixedThreadPool(10); \u53c2\u6570\u8868\u793a\u6700\u591a\u53ef\u4ee5\u8fd0\u884c\u51e0\u4e2a\u7ebf\u7a0b
e.submit(); \u8fd9\u4e2a\u91cc\u9762\u53c2\u6570\u4f20 \u5b9e\u73b0Callable\u63a5\u53e3\u90a3\u4e2a\u7c7b\u7684\u5bf9\u8c61
1、通过继承Thread类创建线程
(1).首先定义一个类去继承Thread父类,重写父类中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。
(2).直接创建一个ThreadTest类的对象,也可以利用多态性,变量声明为父类的类型。
(3).调用start方法,线程启动,隐含的调用run()方法。
[java] view plain copy
public class ThreadTest extends Thread{
public void run(){
for(int i=0;i<=10;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
ThreadTest thread1=new ThreadTest();
ThreadTest thread2=new ThreadTest();
thread1.start();
thread2.start();
}
}
public class ThreadTest implements Runnable{
@Override
public void run() {
for(int i=0;i<=10;i++){
System.out.println(i);
}
}
public static void main(String[] args) {
ThreadTest threadTest=new ThreadTest();
Thread theard=new Thread(threadTest);
theard.start();
}
}
public class ThreadTest implements Callable<Integer>{
@Override
public Integer call() throws Exception {
int count =0;
for(int i=0;i<=10;i++){
count=count+i;
}
return count;
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
ThreadTest test=new ThreadTest();
FutureTask<Integer> thread = new FutureTask<>(test);
new Thread(thread,"有返回值的线程").start();
System.out.println(thread.get());
}
}
- 编程稍微复杂,如果要访问当前线程,则必须使用Thread.currentThread()方法。
2、通过实现Runnable接口创建线程
(1).定义一个类实现Runnable接口,重写接口中的run()方法。在run()方法中加入具体的任务代码或处理逻辑。
(2).创建Runnable接口实现类的对象。
(3).创建一个ThreadTest类的对象,需要封装前面Runnable接口实现类的对象。(接口可以实现多继承)
(4).调用Thread对象的start()方法,启动线程
[java] view plain copy
3.通过Callable和Future创建线程
(1)创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。
(2)创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象封装了该Callable对象的call()方法的返回值。
(3)使用FutureTask对象作为Thread对象的target创建并启动新线程。
(4)调用FutureTask对象的get()方法来获得子线程执行结束后的返回值
[java] view plain copy
使用实现Runnable接口方式创建线程可以共享同一个目标对象(TreadDemo1 tt=new TreadDemo1();),实现了多个相同线程处理同一份资源。
然后再看一段来自JDK的解释:
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments calledrun.
This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example,Runnable is implemented by classThread. Being active simply means that a thread has been started and has not yet been stopped.
In addition, Runnable provides the means for a class to be active while not subclassingThread. A class that implementsRunnable can run without subclassingThread by instantiating aThread instance and passing itself in as the target. In most cases, theRunnable interface should be used if you are only planning to override therun() method and no otherThread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.
采用实现Runnable、Callable接口的方式创见多线程时,优势是:
线程类只是实现了Runnable接口或Callable接口,还可以继承其他类。
在这种方式下,多个线程可以共享同一个target对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU、代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
劣势是:
采用继承Thread类方式:
(1)优点:编写简单,如果需要访问当前线程,无需使用Thread.currentThread()方法,直接使用this,即可获得当前线程。
(2)缺点:因为线程类已经继承了Thread类,所以不能再继承其他的父类。
采用实现Runnable接口方式:
(1)优点:线程类只是实现了Runable接口,还可以继承其他的类。在这种方式下,可以多个线程共享同一个目标对象,所以非常适合多个相同线程来处理同一份资源的情况,从而可以将CPU代码和数据分开,形成清晰的模型,较好地体现了面向对象的思想。
(2)缺点:编程稍微复杂,如果需要访问当前线程,必须使用Thread.currentThread()方法。
绛旓細1銆缁ф壙Thread绫诲垱寤虹嚎绋Thread绫绘湰璐ㄤ笂鏄疄鐜颁簡Runnable鎺ュ彛鐨勪竴涓疄渚嬶紝浠h〃涓涓嚎绋嬬殑瀹炰緥銆傚惎鍔ㄧ嚎绋嬬殑鍞竴鏂规硶灏辨槸閫氳繃Thread绫荤殑start()瀹炰緥鏂规硶銆俿tart()鏂规硶鏄竴涓猲ative鏂规硶锛屽畠灏嗗惎鍔ㄤ竴涓柊绾跨▼锛屽苟鎵цrun()鏂规硶銆傝繖绉嶆柟寮忓疄鐜板绾跨▼寰堢畝鍗曪紝閫氳繃鑷繁鐨勭被鐩存帴extend Thread锛屽苟澶嶅啓run()鏂规硶锛屽氨...
绛旓細杩欏洓绉嶆柟寮忔槸缁ф壙Thread绫汇佸疄鐜癛unnable鎺ュ彛銆佸疄鐜癈allable鎺ュ彛銆佷娇鐢ㄧ嚎绋嬫睜銆1銆佺户鎵縏hread绫伙細閫氳繃缁ф壙Thread绫诲苟閲嶅啓run()鏂规硶鏉ュ疄鐜板绾跨▼銆2銆佸疄鐜癛unnable鎺ュ彛锛氶氳繃瀹炵幇Runnable鎺ュ彛骞堕噸鍐檙un()鏂规硶锛岀劧鍚庡皢Runnable瀵硅薄浣滀负鍙傛暟浼犻掔粰Thread绫荤殑鏋勯犲櫒鏉ュ疄鐜板绾跨▼銆3銆佸疄鐜癈allable鎺ュ彛锛氶氳繃瀹炵幇Callable鎺...
绛旓細(1).棣栧厛瀹氫箟涓涓绫诲幓缁ф壙Thread鐖剁被锛岄噸鍐欑埗绫讳腑鐨剅un()鏂规硶銆傚湪run()鏂规硶涓姞鍏ュ叿浣撶殑浠诲姟浠g爜鎴栧鐞嗛昏緫銆(2).鐩存帴鍒涘缓涓涓猅hreadTest绫荤殑瀵硅薄锛屼篃鍙互鍒╃敤澶氭佹э紝鍙橀噺澹版槑涓虹埗绫荤殑绫诲瀷銆(3).璋冪敤start鏂规硶锛绾跨▼鍚姩锛岄殣鍚殑璋冪敤run()鏂规硶銆俒java] view plain copy public class ThreadTest e...
绛旓細澶氱嚎绋嬪疄鐜扮殑鍥涚鏂瑰紡Thread瑁哥嚎绋嬨丒xecutor鏈嶅姟銆丗orkJoin妗嗘灦銆丄ctor妯″瀷銆1銆乀hread瑁哥嚎绋 绾跨▼鏄苟鍙戞渶鍩烘湰鐨勫崟鍏冦侸ava绾跨▼鏈川涓婅鏄犲皠鍒版搷浣滅郴缁熺嚎绋嬶紝骞朵笖姣忎釜绾跨▼瀵硅薄瀵瑰簲鐫涓涓绠楁満搴曞眰绾跨▼銆傛瘡涓嚎绋嬫湁鑷繁鐨勬爤绌洪棿锛屽畠鍗犵敤浜咼VM杩涚▼绌洪棿鐨勬寚瀹氫竴閮ㄥ垎銆傜嚎绋嬬殑鎺ュ彛鐩稿綋绠鏄庯紝浣犲彧闇瑕佹彁渚涗竴涓猂unnable...
绛旓細java澶氱嚎绋嬬殑鍑犵瀹炵幇鏂瑰紡锛1.缁ф壙Thread绫伙紝閲嶅啓run鏂规硶 2.瀹炵幇Runnable鎺ュ彛锛岄噸鍐檙un鏂规硶锛屽疄鐜癛unnable鎺ュ彛鐨勫疄鐜扮被鐨勫疄渚嬪璞′綔涓篢hread鏋勯犲嚱鏁扮殑target 3.閫氳繃Callable鍜孎utureTask鍒涘缓绾跨▼ 4.閫氳繃绾跨▼姹犲垱寤虹嚎绋 锛堜笂涓绡囧凡缁忚杩囦簡锛夊墠闈袱绉嶅彲浠ュ綊缁撲负涓绫伙細鏃犺繑鍥炲硷紝鍘熷洜寰堢畝鍗曪紝閫氳繃閲嶅啓run鏂规硶锛...
绛旓細//start()鏄疶hread绫讳腑鐨勬柟娉銆俶yt2 m=new myt2();Thread t1= new Thread(m);t1.start();} } /**閫氳繃Callable鍜孎utureTask鍒涘缓绾跨▼ 銆 鍒涘缓Callable鎺ュ彛鐨勫疄鐜扮被 锛屽苟瀹炵幇Call鏂规硶 ;鐢盋allable<Object>鍒涘缓涓涓FutureTask<Object>瀵硅薄;FutureTask<Object>鏄竴涓寘瑁呭櫒锛屽畠閫氳繃鎺ュ彈Callable<Object>...
绛旓細Java澶氱嚎绋嬬殑鍒涘缓鍙婂惎鍔 Java涓嚎绋嬬殑鍒涘缓甯歌鏈夊涓夌鍩烘湰褰㈠紡 1.缁ф壙Thread绫,閲嶅啓璇ョ被鐨剅un()鏂规硶銆 澶嶅埗浠g爜 1 class MyThread extends Thread { 2 3 private int i = 0; 4 5 @Override 6 public void run() { 7 for (i = 0; i < 100; i++) { 8 System.out.println(Thread.currentThrea...
绛旓細鍥涚澶氱嚎绋嬪疄鐜版柟寮忥細1. Thread瑁哥嚎绋 绾跨▼鏄苟鍙戠殑鍩烘湰鍗曞厓锛屾瘡涓嚎绋嬪璞″搴旂潃涓涓绠楁満搴曞眰绾跨▼銆傜嚎绋嬫湁鑷繁鐨勬爤绌洪棿锛屽崰鐢↗VM杩涚▼绌洪棿鐨勬寚瀹氶儴鍒嗐傞氳繃鎻愪緵Runnable骞惰皟鐢╯tart寮濮嬫墽琛岋紝娌℃湁鐜版垚鐨凙PI鏉ョ粨鏉熺嚎绋嬶紝闇瑕佽嚜宸卞疄鐜般傝繖绉嶆柟寮忎紭鐐规槸鎺ヨ繎鎿嶄綔绯荤粺/纭欢妯″瀷鐨勫苟鍙戣绠楋紝浣嗗垱寤虹嚎绋嬮渶瑕佽楄垂澶ч噺鍐呭瓨...
绛旓細姝ラ锛1銆鍒涘缓涓涓被瀹炵幇Runnable鎺ュ彛锛岀劧鍚庨噸鍐檙un鏂规硶 璇风偣鍑昏緭鍏ュ浘鐗囨弿杩 2銆佸垱寤哄疄鐜扮被瀵硅薄銆佷唬鐞嗙被瀵硅薄锛岀劧鍚庝唬鐞嗙被瀵硅薄璋冪敤start()鏂规硶鍚姩绾跨▼ 鐢ㄥ疄鐜癛unnable鎺ュ彛鐨勬柟寮锛屽疄鐜澶氱嚎绋锛氥婃ā鎷熸姠绁ㄧ郴缁熴嬶紝浠g爜濡備笅锛氱嚎绋嬭皟鐢ㄤ簡start()鏂规硶锛屽苟涓嶆剰鍛崇潃绔嬪嵆鎵ц锛岃屾槸鍒板氨缁姸鎬侊紝绛夊緟cpu鐨勮皟搴︼紝鎵浠...
绛旓細1锛鍒涘缓Thread绫荤殑瀛愮被 鍦ㄨ繖涓斿緞涓紝鐢ㄦ埛绋嬪簭闇瑕佸垱寤鸿嚜宸辩殑Thread绫荤殑瀛愮被锛屽苟鍦ㄥ瓙绫讳腑閲嶆柊瀹氫箟鑷繁鐨剅un()鏂规硶锛岃繖涓猺un()鏂规硶涓寘鍚簡鐢ㄦ埛绾跨▼鐨鎿嶄綔銆傝繖鏍峰湪鐢ㄦ埛绋嬪簭闇瑕寤虹珛鑷繁鐨勭嚎绋嬫椂锛屽畠鍙渶瑕佸垱寤轰竴涓宸插畾涔夊ソ鐨凾hread瀛愮被鐨勫疄渚嬪氨鍙互浜嗐備緥锛歱ublic class TestThread{ ...(涓棿鐨勫氨涓嶅啓浜...