首页 > 代码库 > java线程
java线程
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++; } } }
调用service的线程启用方法
//创建线程 推送消息 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);// } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。