2種實現線程的方法:1.繼承Thread類。 2.實現Runnable接口。
多個線程操作同一資源的問題(不同步):第一個線程正操作資源,還沒完成,第二個線程也來操作。
出現了資源錯位。
解決的方法是資源的同步(synchronized)。即:一個線程訪問資源的時候,別的線程不能訪問該資源。
實現同步有2種方法: 1.把方法同步。 2.同步代碼塊。
實現了同步特征:1.線程安全了。 2.性能差了。(異步正好相反)
Object中的wait(),使線程進入等待狀態,需要別的線程notify()繼續工作。
wait()必須要在同步的情況下才能使用。
隨之帶來了死鎖的問題。
面試題:
一、wait()和sleep()區別?
wait方法是Object類中的方法。
sleep方法是Thread類中的方法。
當線程進入wait時別的線程可以訪問鎖定的對象。
當線程進入sleep時別的線程不能訪問鎖定的對象。
二、寫一個死鎖。
class Demo implements Runnable {
int flag ;
static Object o1 = new Object(), o2 = new Object();
public void run(){
System.out.println("線程" + flag + "在運行。。");
if(flag == 1){
synchronized(o1){
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程1 --> o1");
synchronized(o2){
System.out.println("線程1 --> o2");
}
}
}
if(flag == 2){
synchronized(o2){
try {
Thread.sleep(100);
} catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程2 --> o2");
synchronized(o1){
System.out.println("線程2 --> o1");
}
}
}
}
}
結果導致死鎖。
線程1在運行。。
線程2在運行。。
線程1 --> o1
線程2 --> o2
不能繼續進行。
2個Object類為static,說明他們鎖定的是同2個對象;
三、Producer和Consumer的問題
涉及了wait方法和nofity方法
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(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++) {
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++) {
WoTou wt = ss.pop();
System.out.println("消費了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}