java的多线程同步初探

来源:互联网 发布:c语言输出hello world 编辑:程序博客网 时间:2024/04/29 23:42

 

      java在语言层级实现的多线程操作的一些原语,用起来比较方便,不像C、C++之类的要调用操作系统功能才能实现多线程。这里展示一个样例show一下java多线程就是这样简单,一点也不神秘,java让在课本上经常讲的多任务多线程处理很方便的走到初学者的代码中来。(但是也很方便创造deadlock,死锁很难发现,如果不了解就最好不要用,不是必要场合不要用)。至于书上云的一些线程状态以及各状态间的转换则都是一样的,java多了一个线程组,便于以group的方式管理大量线程,更多介绍还是去看API或者google,baidu吧,记录之供参考。如果你能够自己写一遍生产者消费者(也有称读者写者)就有了3、5成功力了吧,那样才能够考虑把它用到你的production代码中去。

      主要的就是一个Thread类,Thread类拥有一个Run()方法,还有一个Thread.start()来启动线程开工。实现多线程有两个方法:

1.将要并行执行的操作封装到一个继承自Thread的类中去

 

public class thread_ex extends  Thread{


    
public void init() {
        }


    
public void start() {
                       
//**will cll run() defualt.
        }


    
public void stop() {

    }


                      
public void run(){
                      
//**do some parallelization works.
                      }


}
    
            

 

2.延伸Runnable接口,满足一些继承了其他类又想多线程的(因为java不像C++一样支持多继承)

 


public class ThreadTest extends FatherClass   implements Runnable
    
public void init() ...{
        }


    
public void start() ...{
                       
//**will cll run() defualt.
        }


    
public void stop() ...{

    }


                      
public void run()...{
                      
//**do some parallelization works.
                      }


}    

与线程有关的几个关键词是很重要的:synchronize  wait   notify     notifyAll

如果不能很好的理解他们还是不要用的好。简单的介绍如下

synchronize  修饰定义一个对象或者方法或者属性的访问必须串行化访问,顾名思义像在银行取钱要排队一样,那个业务员就是一个synchronize 对象。只有像我等无钱之辈才去排队,那些个VIP自然不用了。

wait  和notify  notifyAll是一家的,他们是对象级的锁,余天生驽钝,这个偶翻了许多资料才理解,如果不能理解就看代码吧。多线程中的一个去访问共享资源发现有人使用,于是就在这个资源上面wait,占用者如果使用完了就notify()一下通知下一个等待者可以进来了,notifyAll()就是说在外面等的一起上吧!可是只能服务与一个人(不能玩3P  -_-#),于是大家抢着进去,力气大的自然占便宜。可是在java世界里面JVM说了算。

有wait和notify的地方一定有synchronize,反过来不成立,wait 和notify不在Thread的势力范围

            说正题,直奔代码!本例设计到三个类:ThreadController,PrintThread,Resource;也是顾名思义就不做解释。放代码:

 

        public class Resource{
        
public static boolean inUse;
        
//public  boolean inUse=false;
        String name;
        
int sleepTime=0;
        
        
public synchronized void out(String n){
            
this.name=n;
        
        
while(Resource.inUse==true
        
        
try{
         System.out.println(
"Name:"+getName()+" Waiting...");
         
this.wait();
       
        }
catch(InterruptedException ie){
         ie.printStackTrace();
        }

        Resource.inUse
=true;        // entry the block  and lock it
        try{
        sleepTime
=(int)(Math.random()*5000);
    System.out.println(
"Name:"+getName()+" in use; define Sleep Time: "+sleepTime+",wait 50000 to exit the synchronize block");
    Thread.sleep(
5000);   
        System.out.println(
"Name:"+getName()+"  end 5000 sleep . Sleep some time after notify to test if others can end first");
        Thread.sleep(
5000-sleepTime);
        System.out.println(
"Name:"+getName()+" will Exit the synchronize block");
        Resource.inUse
=false;
        System.out.println(
"Name:"+getName()+" End. "+Resource.inUse);
        
this.notifyAll();
        }
catch(InterruptedException ie){
         ie.printStackTrace();
        }
      
        }

        
public String getName(){
        
return name;
        }

        
        }
public class ThreadController{
     
public static void main(String args[]){
     PrintThread t1,t2,t3,t4;
     Resource r
=new Resource();
     Resource.inUse
=false;
     t1 
= new PrintThread("1",r);
     t2 
= new PrintThread("2",r);
     t3 
= new PrintThread("3",r);
     t4 
= new PrintThread("4",r);
     t1.start();
     t2.start();
     t3.start();
     t4.start();
     System.out.println(
"Controller ended...");
     
//System.exit(0);
     }


}

 

public class PrintThread extends Thread{
    
int sleepTime;
    Resource rs;
    
public PrintThread(String id,Resource r){
    
super(id);
    
this.rs=r;
    }

    
public void run(){
    
try{
    System.out.println(
"Thread :"+getName()+"  start...... wait to entry synchronize block");
    rs.out(getName());
    sleepTime
=(int)(Math.random()*5000);
    System.out.println(
"Thread :"+getName()+"  start to sleep"+sleepTime+"mms");
    sleep(sleepTime);
           }
catch(InterruptedException ie){
    ie.printStackTrace(); 
         }

         System.out.println(
"Thread :"+getName()+"  wakeup, end.");
    }

    
    
        
        
        

}




 

点击这里查看测试输出结果,输出了两遍,以便体现出多线程的不可重现特性: