柚子快報(bào)激活碼778899分享:jvm 創(chuàng)建線程的四種方式
柚子快報(bào)激活碼778899分享:jvm 創(chuàng)建線程的四種方式
1.繼承Thread類,重寫run()方法; 2.實(shí)現(xiàn)Runnable接口,重寫run()方法; 3.實(shí)現(xiàn)Callable接口,重寫call()方法; 4.使用線程池創(chuàng)建線程;
一、繼承Thread類 步驟: 1.創(chuàng)建一個(gè)類繼承Thread類,重寫其run()方法,在run()方法中編寫需要執(zhí)行的任務(wù); 2.創(chuàng)建繼續(xù)Thread類的對象; 3.調(diào)用其對象的start()方法,該方法表示啟動(dòng)一個(gè)線程,線程處于就緒狀態(tài),如果獲取到cpu執(zhí)行權(quán),則會調(diào)用run()方法,執(zhí)行對應(yīng)的任務(wù);
public class ThreadDemo extends Thread{
private Thread t;
private String threadName;
ThreadDemo(String name)
{
threadName = name;
System.out.println("創(chuàng)建" + threadName );
}
@Override
public void run(){
System.out.println("運(yùn)行"+threadName);
try{
for(int i = 4; i > 0; i--){
System.out.println("線程"+threadName+","+i);
Thread.sleep(50);
}
}catch (InterruptedException e){
System.out.println("線程"+threadName+"中斷");
}
System.out.println("線程"+threadName+"退出");
}
public void start(){
System.out.println("啟動(dòng)"+threadName);
if(t==null){
t = new Thread(this,threadName);
t.start();
}
}
}
public class ThreadTest {
public static void main(String[] args) {
ThreadTest();
}
private static void ThreadTest() {
ThreadDemo T1 = new ThreadDemo("Thread-->1");
T1.start();
ThreadDemo T2 = new ThreadDemo("Thread-->2");
T2.start();
}
}
二、實(shí)現(xiàn)Runnable接口 步驟: 1.創(chuàng)建一個(gè)類,實(shí)現(xiàn)Runnable接口,重寫run()方法,編寫需要執(zhí)行的代碼; 2.創(chuàng)建一個(gè)Runnable接口實(shí)現(xiàn)類的對象; 3.將此對象作為形參傳遞到Thread類的構(gòu)造器中,創(chuàng)建Thread類的對象,調(diào)用start()方法,啟動(dòng)線程;
public class RunnableDemo implements Runnable{
private Thread t;
private String threadName;
RunnableDemo(String name){
threadName = name;
System.out.println("創(chuàng)建 " + threadName);
}
@Override
public void run() {
System.out.println("運(yùn)行 " + threadName);
try {
for (int i = 4; i > 0; i--) {
System.out.println("線程 " + threadName + ", " + i);
Thread.sleep(50);
}
}catch (InterruptedException e){
System.out.println("線程 " + threadName + " 運(yùn)行中");
}
System.out.println("線程 " + threadName + " 運(yùn)行結(jié)束");
}
public void start(){
System.out.println("啟動(dòng)線程"+ threadName);
if(t==null){
t=new Thread(this,threadName);
t.start();
}
}
}
public class ThreadTest {
public static void main(String[] args) {
runnableTest();
}
private static void runnableTest() {
RunnableDemo R1=new RunnableDemo("runnableThread-->1");
R1.start();
RunnableDemo R2=new RunnableDemo("runnableThread-->2");
R2.start();
}
}
三、實(shí)現(xiàn)Callable接口 步驟: 1.創(chuàng)建Callable接口的實(shí)現(xiàn)類,并實(shí)現(xiàn)call()方法,該方法具有返回值,并且可以拋異常; 2.創(chuàng)建Callable實(shí)現(xiàn)類的實(shí)例,并使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值; 3.使用FutureTask對象作為Thread對象的target創(chuàng)建并啟動(dòng)新線程; 4.調(diào)用FutureTask對象的get()方法來獲取子線程執(zhí)行結(jié)束后的返回值;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
public class CallableThreadTest implements Callable
public static void main(String[] args)
{
CallableThreadTest ctt = new CallableThreadTest();
FutureTask
for(int i = 0;i < 10;i++)
{
System.out.println(Thread.currentThread().getName()+" 的循環(huán)變量i的值"+i);
if(i==5)
{
new Thread(ft,"有返回值的線程").start();
}
}
try
{
System.out.println("子線程的返回值:"+ft.get());
} catch (InterruptedException e)
{
System.out.println("發(fā)生異常");
} catch (ExecutionException e)
{
System.out.println("發(fā)生異常");
}
}
@Override
public Integer call() throws Exception
{
int i = 0;
for(;i<10;i++)
{
System.out.println(Thread.currentThread().getName()+" "+i);
}
return i;
}
}
Callable 和 Runnable接口的區(qū)別
Callable規(guī)定的方法是call(),而Runnable規(guī)定的方法是run()。Callable的任務(wù)執(zhí)行后可返回值,而Runnable的任務(wù)是不能返回值的。call()方法可拋出異常,而run()方法是不能拋出異常的。
?四,線程池創(chuàng)建線程
我這里用newFixedThreadPool:創(chuàng)建一個(gè)固定數(shù)量的線程池
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPool {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 2; i++) {
executor.execute(new Runnable() {
@Override
public void run() {
System.out.println("線程名: " + Thread.currentThread().getName() + " is running");
}
});
}
}
}
柚子快報(bào)激活碼778899分享:jvm 創(chuàng)建線程的四種方式
精彩內(nèi)容
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。