Java多線程學習總結(二)
一、interrupt方法一種讓線程退出的方式。
- import java.util.*;
- public class TestInterrupt{
- public static void main(String[] args){
- MyThread t = new MyThread();
- t.start();
- try{Thread.sleep(10000);}
- catch(InterruptedException i){}
- t.interrupt();
- }
- }
- class MyThread extends Thread{
- public void run(){
- while(true){
- try{
- System.out.println("------"+new Date()+"-----");
- Thread.sleep(1000);
- }catch(InterruptedException i){
- return;
- }
- }
- }
- }
二、join和yield方法
t.join(); //t的run()方法完才會繼續執行當前線程方法體
//也就是兩個線程變成了一個線程
t.yield(); //暫停當前正在執行的線程對象,并執行其他線程。方法為靜態
//哪個線程體執行此方法,哪個線程讓步
- public class TestYield {
- public static void main(String[] args) {
- MyThread3 t1 = new MyThread3("t1");
- MyThread3 t2 = new MyThread3("t2");
- t1.start(); t2.start();
- }
- }
- class MyThread3 extends Thread {
- MyThread3(String s){super(s);}
- public void run(){
- for(int i =1;i<=100;i++){
- System.out.println(getName()+": "+i);
- if(i%10==0){
- yield();
- }
- }
- }
- }
三、線程優先級別
線程的優先級用數字表示,范圍從1到10,一個線程的缺省優先級為5.
Thread.MAX_PRIORITY=1
Thread.MIN_PRIORITY=10
Thread.NORM_PRIORITY=5
例:t.setPriority(Thread.NORM_PRIORITY+3);
四、線程同步
1.同步代碼塊
synchronized(this){ //在執行代碼塊過程中,不會被其他線程打斷
...
}
public sunchronized void method //執行此方法時,當前對象被鎖定
在Java語言中,引入了對象互斥鎖的概念,保證共享數據操作的完整性,每個對象 都對應一個可稱為"互斥鎖"的標記,這個標記保證在任一時刻,只能有一個線程訪 問該對象。
2.線程死鎖
- public class TestDeadLock implements Runnable {
- public int flag = 1;
- static Object o1 = new Object(), o2 = new Object();
- public void run() {
- System.out.println("flag=" + flag);
- if(flag == 1) {
- synchronized(o1) {
- try {
- Thread.sleep(500);
- } catch (Exception e) {
- e.printStackTrace();
- }
- synchronized(o2) {
- System.out.println("1");
- }
- }
- }
- if(flag == 0) {
- synchronized(o2) {
- try {
- Thread.sleep(500);
- } catch (Exception e) {
- e.printStackTrace();
- }
- synchronized(o1) {
- System.out.println("0");
- }
- }
- }
- }
- public static void main(String[] args) {
- TestDeadLock td1 = new TestDeadLock();
- TestDeadLock td2 = new TestDeadLock();
- td1.flag = 1;
- td2.flag = 0;
- Thread t1 = new Thread(td1);
- Thread t2 = new Thread(td2);
- t1.start();
- t2.start();
- }
- }
五、生產者消費者問題
- public class ProducerConsumer {
- public static void main(String[] args) {
- SyncStack ss = new SyncStack();
- Producer p = new Producer(ss);
- Consumer c = new Consumer(ss);
- new Thread(p).start();
- new Thread(p).start();
- new Thread(p).start();
- new Thread(c).start();
- }
- }
- class WoTou {
- int id;
- WoTou(int id) {
- this.id = id;
- }
- public String toString() {
- return "WoTou : " + id;
- }
- }
- class SyncStack { //棧實現
- int index = 0;
- WoTou[] arrWT = new WoTou[6]; //相當于裝物品的籃子
- public synchronized void push(WoTou wt) { //生產物品,線程安全
- while(index == arrWT.length) { //當籃子滿了線程等待
- try {
- this.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.notifyAll(); //開始生產時,叫醒等待的其他線程開始消費
- arrWT[index] = wt;
- index ++;
- }
- public synchronized WoTou pop() { //消費物品,線程安全
- while(index == 0) { //如果籃子空了
- try {
- this.wait(); //線程等待,等待生產者開始
- //生產,叫醒此線程
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.notifyAll(); //消費時喊醒生產者生產
- index--;
- return arrWT[index];
- }
- }
- class Producer implements Runnable { //生產者類
- SyncStack ss = null;
- Producer(SyncStack ss) {
- this.ss = ss;
- }
- public void run() {
- for(int i=0; i<20; i++) { //生產20個
- WoTou wt = new WoTou(i);
- ss.push(wt);
- System.out.println("生產了:" + wt);
- try {
- Thread.sleep((int)(Math.random() * 200));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class Consumer implements Runnable {
- SyncStack ss = null;
- Consumer(SyncStack ss) {
- this.ss = ss;
- }
- public void run() {
- for(int i=0; i<20; i++) { //消費20個
- WoTou wt = ss.pop();
- System.out.println("消費了: " + wt);
- try {
- Thread.sleep((int)(Math.random() * 1000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
【編輯推薦】