黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

Java面向?qū)ο?/div>
Java異常
Java數(shù)組
Java常用類
Java集合
Java IO流
Java線程
Java反射
Socket編程
Java注解開發(fā)
Java GoF設(shè)計模式
HashMap
Java內(nèi)存模型
Java線性表

Java創(chuàng)建線程的方式

Java創(chuàng)建線程依賴java.lang.Thread類

定義Thread類子類

package com.wkcto.chapter07.newthread;
/**
 * 演示創(chuàng)建線程的方式一
 * 		定義Thread類的子類
 * @author 蛙課網(wǎng)
 *
 */
public class Test01 {

	public static void main(String[] args) {
		//3)創(chuàng)建線程對象
		SubThread t1 = new SubThread();
		//4) 開啟線程
		t1.start(); 		//開啟的線程會執(zhí)行run()方法
//		t1.run();  			//就是普通實例方法的調(diào)用, 不會開啟新的線程
		
		//當(dāng)前線程是main線程 
		for( int i = 1;  i<=100; i++){
			System.out.println( "main : " + i);
		}
		/*
		 * 每次運行程序, 運行的結(jié)果可能不一樣
		 * 運行程序后, 當(dāng)前程序就有兩個線程main線程和t1線程在同時執(zhí)行, 這兩個線程中哪個線程搶到CPU執(zhí)行權(quán), 這個線程就執(zhí)行
		 * 	
		 * 	在單核CPU中, 在某一時刻CPU只能執(zhí)行一個任務(wù), 因為CPU執(zhí)行速度非常快, 可以在各個線程之間進(jìn)行快速切換
		 * 	對于用戶來說, 感覺是多個線程在同時執(zhí)行
		 * 	
		 */
	}

}

//1)定義類繼承Thread
class SubThread extends Thread{
	//2)重寫run(), run()方法中的代碼就是子線程要執(zhí)行的代碼
	@Override
	public void run() {
		//在子線程中打印100行字符串
		for( int i = 1; i<=100 ; i++){
			System.out.println("sub thread -->" + i);
		}
	}
}

定義Runnable接口的實現(xiàn)類

package com.wkcto.chapter07.newthread;
/**
 * 創(chuàng)建線程的方式二
 * 	實現(xiàn)Runnable接口
 * @author 蛙課網(wǎng)
 *
 */
public class Test02 {

	public static void main(String[] args) {
		//3) 創(chuàng)建線程對象, 調(diào)用構(gòu)造方法 Thread(Runnable) , 在調(diào)用時, 傳遞Runnable接口的實現(xiàn)類對象
		Prime p = new Prime(); 		//創(chuàng)建Runnable接口的實現(xiàn)類對象
		Thread t2 = new Thread(p);
		//4) 開啟線程
		t2.start();

		//通過Runnable接口匿名內(nèi)部類的形式創(chuàng)建線程
		Thread t22 = new Thread(new Runnable() {
			@Override
			public void run() {
				for( int i = 1; i <= 100; i++){
					System.out.println("t22==> " + i);
				}
			}
		});
		t22.start();
		
		// 當(dāng)前線程是main線程
		for (int i = 1; i <= 100; i++) {
			System.out.println("main : " + i);
		}
	}

}
//1) 定義一個類實現(xiàn)Runnable接口
class Prime implements Runnable{
	//2)重寫run()方法, run()方法中的代碼就是子線程要執(zhí)行的代碼
	@Override
	public void run() {
		// 在子線程中打印100行字符串
		for (int i = 1; i <= 100; i++) {
			System.out.println("sub thread -->" + i);
		}
	}

}

定義Callable接口的實現(xiàn)類

package com.wkcto.chapter07.newthread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 創(chuàng)建線程的方式三
 * 		實現(xiàn)Callable接口
 * @author 蛙課網(wǎng)
 *
 */
public class Test03 {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		//3)創(chuàng)建線程對象
		Prime2 p2 = new Prime2(); 		//創(chuàng)建Callable接口的實現(xiàn)類對象
		FutureTask<Integer> task = new FutureTask<>( p2 ); 		//創(chuàng)建FutureTask對象
		//FutureTask類實現(xiàn)了RunnableFuture接口, 該接口繼承了Runnable接口, FutureTask類就是Runnable接口的實現(xiàn)類
		Thread t3 = new Thread(task); 
		
		//4)開啟線程
		t3.start();
		
		// 當(dāng)前線程是main線程
		for (int i = 1; i <= 100; i++) {
			System.out.println("main : " + i);
		}
		
		//在main線程中可以取得子線程的返回值
		System.out.println(" task result : " + task.get() );
	}

}
//1)定義類實現(xiàn)Callable接口
//Callable接口的call()方法有返回值, 可以通過Callable接口泛型指定call()方法的返回值類型
class Prime2 implements Callable<Integer> {
	//2)重寫call()方法, call()方法中的代碼就是子線程要執(zhí)行的代碼
	@Override
	public Integer call() throws Exception {
		//累加1~100之間的整數(shù)和
		int sum = 0 ; 
		for(int i = 1; i<=100; i++){
			sum += i;
			System.out.println("sum=" + sum);
		}
		return sum;
	}
	
}

 

主站蜘蛛池模板: 日本韩国在线播放 | 亚洲国产日韩欧美在线a乱码 | 亚洲日韩欧美一区二区在线 | 国产综合成人亚洲区 | 免费可以看黄的视频 s色 | 在线人成精品免费视频 | 毛片免费播放 | 亚洲影视一区 | 欧美日韩在线一区二区三区 | 在线观看国产高清免费不卡黄 | www.狠狠插| 欧美成人精品久久精品 | 日韩视频免费 | 日本在线网 | 久久青青国产 | 天天综合天天 | 国产精品综合 | 日韩另类| 亚洲成年网站在线777 | 天天干天天插天天 | 欧美日韩综合高清一区二区 | 91网站国产 | 欧美性猛交xxxxx免费看 | 亚洲欧美日本韩国综合在线观看 | 欧美午夜理伦三级理论三级 | 91在线看片一区国产 | 最近的2019中文字幕4 | 日本大黄视频 | 一级二级三级黄色片 | 日本香蕉视频在线观看 | 国产精品一区二区在线观看 | 中文字幕25页 | 91高清国产经典在线观看 | 亚洲一区区 | 中国xxx农村性视频 中国a毛片 | 亚洲视频精品在线 | 欧美日韩一区二区三区在线播放 | 成人婷婷 | 人人做人人澡人人人爽 | 日本亚洲国产 | 一级黄免费 |