首页 > 编程语言 > 详细

java线程

时间:2014-05-08 17:53:51      阅读:343      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
package com.test;   
import java.util.Date;   
/**  
 * @author libt
 * @version 创建时间:2014-5-08上午11:39:37  
 * 开了三个线程。一个timeThread,一个thread2,一个main thread。其中只有等timeThread执行完了,才能执行main thread  
 */  
public class ThreadTest {   
    public static void main(String[] args) {   
           
           
        TimeRunnable timeRunnable = new TimeRunnable("time thread");   
        Thread timeThread = new Thread(timeRunnable);   
           
        Thread2 thread2 = new Thread2("wangking");   
        thread2.setPriority(Thread.NORM_PRIORITY+2);   
        timeThread.start();   
        thread2.start();   
           
        try {   
            timeThread.join();   
        } catch (InterruptedException e) {   
            e.printStackTrace();   
        }   
           
        for(int i=0;i<100;i++){   
            System.out.println("-------main is running-----"+i);   
        }   
           
           
    }   
       
}   
//线程一   
class Thread2 extends Thread{   
       
    boolean flag = true;   
       
    public Thread2(String name){   
        super(name);   
    }   
       
    @Override  
    public void run() {   
           
        for(int i = 0;i < 100;i++){   
            System.out.println(getName()+" thread is running:" + i);   
            if(i%10 == 0){   
                try {   
                    this.sleep(1000);   
                } catch (InterruptedException e) {   
                    e.printStackTrace();   
                    return;   
                }   
                this.yield();   
            }   
        }   
           
    }   
       
    //线程二   
    public void setFlag(boolean flag){   
        this.flag = flag;   
    }   
       
}   
class TimeRunnable implements Runnable{   
       
    private String name;    //线程名称   
       
    public TimeRunnable(String name){   
        this.name = name;   
    }   
       
    public void run() {   
           
        int i = 0;   
        while(i<=10){   
               
            System.out.println("----"+this.name+" at "+new Date());   
               
            try {   
                Thread.sleep(1000);   
            } catch (InterruptedException e) {   
                return;   
            }   
            i++;   
               
        }      
           
           
           
    }   
       
} 
bubuko.com,布布扣

 调用service的线程启用方法

bubuko.com,布布扣
//创建线程 推送消息
new MessageAllThread("pushId",pushMessageWebService).start();


import org.springframework.stereotype.Component;
import com.rimi.tpl.web.system.service.PushMessageWebService;

@Component
public class MessageAllThread extends Thread {

    private String pushId;
    private PushMessageWebService pushMessageWebService;
    
    public MessageAllThread(){}
    
    public MessageAllThread(String pushId, PushMessageWebService pushMessageWebService){
        this.pushId = pushId;
        this.pushMessageWebService = pushMessageWebService;
    }

    public void run() {
        pushMessageWebService.pushToAllbyPushId(pushId);//
    }

}
bubuko.com,布布扣

 

 

java线程,布布扣,bubuko.com

java线程

原文:http://www.cnblogs.com/libaoting/p/javaThread.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!