(1)
package day20150907thread;
/**
* 線程調度分配給cpu的時間片段給不同的線程
* 得到時間片段的線程被cpu運行,其他線程等待
* 線程調度會儘可能均勻的分配時間片段給不同的進程
*
* 經紀人分配給劇組不同的檔期,讓葛大爺去演戲
* 線程調度分配給不同的線程時間片段,讓cpu去運行
*/
public class ThreadDemo1 {
/**
* 第一種創建線程的方式
* 繼承Thread類,重寫run方法
*
* 寫起來簡單,但用起來沒有實現Runnable靈活
*/
public static void main(String[] args) {
//有先後順序的運行方式叫同步運行(同一個線程)
//各幹各的叫異步運行(多個線程裏執行)
Thread t1 = new Thread1();
Thread t2 = new Thread2();
/**
* start方法用於將線程納入線程調度
* 這時,線程處於runnable狀態
* 等待線程調度分配時間片段
* 當線程調度將時間片段給配給當前線程
* 該線程的run方法才被執行
* 直到run方法執行完畢,線程結束最終被回收
* 在線程的run方法執行期間,該線程處於走走停停
*/
t1.start();
t2.start();
}
}
//Thread:實現類,已經實現Runnable接口
class Thread1 extends Thread{
@Override
public void run() {
for(int i=0;i<5000;i++){
System.out.println("誰啊");
}
}
}
class Thread2 extends Thread{
@Override
public void run(){
for(int i=0;i<5000;i++){
System.out.println("我");
}
}
}
(2)
package day20150907thread;
/**
* 第二種創建線程方式:
* 定義線程體Runnable
*/
public class ThreadDemo2 {
public static void main(String[] args) {
Runnable r1 = new MyRunnable1();
Runnable r2 = new MyRunnable2();
// r1.run();
// r2.run();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
class MyRunnable1 implements Runnable{
@Override
public void run() {
for(int i=0;i<5000;i++){
System.out.println("誰啊?");
}
}
}
class MyRunnable2 implements Runnable{
@Override
public void run() {
for(int i=0;i<5000;i++){
System.out.println("我");
}
}
}
(3)
package day20150907thread;
public class ThreadDemo3 {
/**
* 使用匿名內部類的方式創建線程
*/
public static void main(String[] args) {
Thread t1 = new Thread(){
public void run(){
for(int i=0;i<5000;i++){
System.out.println("11111111111");
}
}
};
Thread t2 = new Thread(
new Runnable(){
public void run(){
for(int i=0;i<5000;i++){
System.out.println("22");
}
}
}
);
t1.start();
t2.start();
}
}
(4)
package day20150907thread;
/**
* 獲取執行當前代碼片段的線程
* 可以調用Thread提供的靜態方法
* Thread Thread.currentThread()
*/
public class ThreadDemo4 {
public static void main(String[] args) {
Thread current = Thread.currentThread();
System.out.println("main線程:"+current);
testCurrentThread();
Thread t1 = new Thread(){
public void run(){
Thread myt = Thread.currentThread();
System.out.println("myt:"+myt);
testCurrentThread();
}
};
t1.start();
}
public static void testCurrentThread(){
Thread t = Thread.currentThread();
System.out.println("方法線程:"+t);
}
}
(5)
package day20150907thread;
/**
* ID:非空並且唯一
* 線程其他API
*/
public class ThreadDemo5 {
public static void main(String[] args) {
//獲取調用main方法的相關信息
Thread t = Thread.currentThread();
//獲取線程id,通常由系統分配
long id = t.getId();
System.out.println("ID"+id);
/**
* 獲取線程名字
* 格式爲Thread-X
* 但是main方法的線程不是這個,爲main
*/
String name = t.getName();
System.out.println("線程名字:"+name);//main
int p = t.getPriority();
System.out.println("線程優先級:"+p);
System.out.println("線程狀態"+t.getState());
System.out.println("線程是否活着:"+t.isAlive());
System.out.println("是否後臺進程:"+t.isDaemon());
System.out.println("線程是否被中斷:"+t.isInterrupted());
}
}
(6)
package day20150907thread;
/**
* 線程優先級
* 1-10
* 理論上,線程優先級高的線程
* 被分配的時間片段次數多
*/
public class ThreadDemo6 {
public static void main(String[] args) {
Thread max = new Thread(){
public void run(){
for(int i=0;i<5000;i++){
System.out.println("maxmaxmaxmaxmaxmaxmaxmax");
}
}
};
Thread min = new Thread(){
public void run(){
for(int i=0;i<5000;i++){
System.out.println("min");
}
}
};
Thread nor = new Thread(){
public void run(){
for(int i=0;i<5000;i++){
System.out.println("normal");
}
}
};
max.setPriority(Thread.MAX_PRIORITY);//或者max.setPriority(10);
min.setPriority(Thread.MIN_PRIORITY);//或者min.setPriority(1);
max.start();
min.start();
nor.start();
}
}
(7)
package day20150907thread;
/**
* 後臺線程(守護線程)
* 當進程中的所有前臺線程都結束時,後臺線程結束
* 無論後臺線程是否還在運行
*/
public class ThreadDemo7 {
public static void main(String[] args) {
//rose:前臺進程
Thread rose = new Thread(){
public void run(){
for(int i=0;i<8;i++){
System.out.println("Go");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
//jack:守護進程
Thread jack = new Thread(){
public void run(){
while(true){
System.out.println("Jack: you go, I go!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
rose.start();
jack.setDaemon(true);
jack.start();
//while(true);//此死循環沒執行完的話,main方法不結束,守護進程也不結束
//main方法執行完
System.out.println("main方法執行完了");
}
}
(8)
package day20150907thread;
import java.text.SimpleDateFormat;
import java.util.Date;
//(8)sleep 阻塞
public class ThreadDemo8 {
/**
* 實現電子錶(15:13:24)
*/
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
while(true){
Date now = new Date();
System.out.println(sdf.format(now));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
(9)
package day20150907thread;
/**
* join:線程同步
*
* 注:
* 解決多線程併發安全問題的辦法是:
* 將異步的操作變成同步的
*
* 產生多線程併發的原因是:
* 多線程併發操作同一數據
*/
public class ThreadDemo9 {
/**
* 圖片是否下載完
*/
public static boolean isFinish;
public static void main(String[] args) {
final Thread download = new Thread(){
public void run(){
System.out.println("開始下載圖片。。。");
for(int i=1;i<=100;i++){
System.out.println(i+"%");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("下載完成");
isFinish = true;
}
};
/*
* main方法中定義了一個內部類show
* 該內部類中若想引用main方法中的其他局部變量
* 那麼這個變量必須是final的
*/
Thread show = new Thread(){
public void run(){
System.out.println("開始顯示圖片");
//這裏等待圖片下載完成
try {
download.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if(!isFinish){
throw new RuntimeException("圖片還沒有下載完成");
}
System.out.println("圖片打開");
}
};
download.start();
show.start();
}
}