Java多線程學(xué)習(xí)總結(jié)(二)
一、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()方法完才會(huì)繼續(xù)執(zhí)行當(dāng)前線程方法體
//也就是兩個(gè)線程變成了一個(gè)線程
t.yield(); //暫停當(dāng)前正在執(zhí)行的線程對(duì)象,并執(zhí)行其他線程。方法為靜態(tài)
//哪個(gè)線程體執(zhí)行此方法,哪個(gè)線程讓步
- 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();
- }
- }
- }
- }
三、線程優(yōu)先級(jí)別
線程的優(yōu)先級(jí)用數(shù)字表示,范圍從1到10,一個(gè)線程的缺省優(yōu)先級(jí)為5.
Thread.MAX_PRIORITY=1
Thread.MIN_PRIORITY=10
Thread.NORM_PRIORITY=5
例:t.setPriority(Thread.NORM_PRIORITY+3);
四、線程同步
1.同步代碼塊
synchronized(this){ //在執(zhí)行代碼塊過(guò)程中,不會(huì)被其他線程打斷
...
}
public sunchronized void method //執(zhí)行此方法時(shí),當(dāng)前對(duì)象被鎖定
在Java語(yǔ)言中,引入了對(duì)象互斥鎖的概念,保證共享數(shù)據(jù)操作的完整性,每個(gè)對(duì)象 都對(duì)應(yīng)一個(gè)可稱(chēng)為"互斥鎖"的標(biāo)記,這個(gè)標(biāo)記保證在任一時(shí)刻,只能有一個(gè)線程訪 問(wèn)該對(duì)象。
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();
- }
- }
五、生產(chǎn)者消費(fèi)者問(wèn)題
- 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 { //棧實(shí)現(xiàn)
- int index = 0;
- WoTou[] arrWT = new WoTou[6]; //相當(dāng)于裝物品的籃子
- public synchronized void push(WoTou wt) { //生產(chǎn)物品,線程安全
- while(index == arrWT.length) { //當(dāng)籃子滿了線程等待
- try {
- this.wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.notifyAll(); //開(kāi)始生產(chǎn)時(shí),叫醒等待的其他線程開(kāi)始消費(fèi)
- arrWT[index] = wt;
- index ++;
- }
- public synchronized WoTou pop() { //消費(fèi)物品,線程安全
- while(index == 0) { //如果籃子空了
- try {
- this.wait(); //線程等待,等待生產(chǎn)者開(kāi)始
- //生產(chǎn),叫醒此線程
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.notifyAll(); //消費(fèi)時(shí)喊醒生產(chǎn)者生產(chǎn)
- index--;
- return arrWT[index];
- }
- }
- class Producer implements Runnable { //生產(chǎn)者類(lèi)
- SyncStack ss = null;
- Producer(SyncStack ss) {
- this.ss = ss;
- }
- public void run() {
- for(int i=0; i<20; i++) { //生產(chǎn)20個(gè)
- WoTou wt = new WoTou(i);
- ss.push(wt);
- System.out.println("生產(chǎn)了:" + 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++) { //消費(fèi)20個(gè)
- WoTou wt = ss.pop();
- System.out.println("消費(fèi)了: " + wt);
- try {
- Thread.sleep((int)(Math.random() * 1000));
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
【編輯推薦】