線程的2種實現和線程的交替執行,線程2種交替執行

來源:互聯網
上載者:User

線程的2種實現和線程的交替執行,線程2種交替執行

學了線程,收穫不少,記錄下了吧.

一、線程的主要兩種實現方法。

1.繼承Thread類,重寫run()方法  

   main方法中建立子類,引用調用start()方法

執行個體如下:

//繼承Thread類,重寫run()方法      

public class ThreadOne extends Thread {

public void run() {
for (int i = 1; i <=100; i++) {
System.out.println(this.getName()+":"+i);
}
}

public static void main(String[] args) {
//建立Thread對象
ThreadOne threadOne = new ThreadOne();
//調用start()
threadOne.start();

}

}

2.實現Runnable()介面,實現run()方法。

 mian方法中 執行個體該類,執行個體Thread類並持有該類的引用 Thread的引用調用start().

執行個體:

//實現Runnable()介面,實現run()方法

public class MyThread implements Runnable {
public void run() {
for(int i=0; i<10; i++){
System.out.println(Thread.currentThread().getName()+"放入一個蘋果:"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}

public static void main(String[] args) {

//建立MyThread類
MyThread myThread =new MyThread();

//建立Thread,並持有MyThread的應用
Thread thread = new Thread(myThread);

//調用start()
thread.start();
}
}

二.規律交替執行線程題目

例如問題:實現兩個線程能夠交替每隔1秒鐘列印一次時間,每個線程列印5次後結束線程

結果要求:

Thread-0 Sun Feb 28 11:21:52 CST 2016;
Thread-1 Sun Feb 28 11:21:53 CST 2016
Thread-0 Sun Feb 28 11:21:54 CST 2016
Thread-1 Sun Feb 28 11:21:55 CST 2016
Thread-0 Sun Feb 28 11:21:56 CST 2016
Thread-1 Sun Feb 28 11:21:57 CST 2016
Thread-0 Sun Feb 28 11:21:58 CST 2016
Thread-1 Sun Feb 28 11:21:59 CST 2016
Thread-0 Sun Feb 28 11:22:00 CST 2016
Thread-1 Sun Feb 28 11:22:01 CST 2016

問題分析:

首先要 實現2個線程:功能都是建立時間;其次需要交替執行,就必須加鎖synchronized;這些都是可以理解的

重要的是這個鎖怎麼控制,用什麼控制,採用方法為:定義一個static狀態值 state ,取值為1,2;

當state=1的時候,執行線程1,線程2等待,執行結束後將state=2,並調用notifyAll() 或者notify()方法;

當state=2的時候,執行線程2,線程1等待,執行結束後將state=1,並調用notifyAll() 或者notify()方法;

最後就剩控制5次的問題了,各自訂2個static int 變數 利用while控制唄,各線程執行一次,相應的變數值加1到5結束

代碼:

public class ThreadDate{
//狀態值 控制 執行哪個程式用
private static int state =1;
//控制線程一執行的次數
private static int num1 =1;
//控制線程2執行的次數
private static int num2 =1;
public static void main(String[] args) {
//用該類作為鎖
final ThreadDate t=new ThreadDate();
//建立第一個線程,並調用start()方法
new Thread(new Runnable() {
public void run() {
//用while num1控制執行次數
while(num1<=5){
//加鎖
synchronized (t) {
//首先應狀態值判斷是不是該執行
if(state!=1){
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" "+new Date());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//執行結束,狀態值改變
state =2;
t.notifyAll();
}
num1++;
}
}
}).start();;

//建立第二個線程,並調用start()方法
new Thread(new Runnable() {
public void run() {
while(num2<=5){
synchronized (t) {
if(state!=2){
try {
t.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+" "+new Date());

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

state =1;
t.notifyAll();
}
num2++;
}
}
}).start();
}
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.