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();
            }            
        }
    }
}

posted on 2008-12-30 13:51 天长 阅读(123) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: