zoukankan      html  css  js  c++  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);//
        }
    
    }
  • 相关阅读:
    SpreadJS 复制行
    RookeyFrame 模块 线上创建的模块 迁移到 线下来
    RookeyFrame 附件 上传附件
    RookeyFrame 字典 新增和绑定
    RookeyFrame Bug 表单管理 -> 查看表单 ->编辑字段页面 JS报错
    Catalan数
    美元汇率
    5倍经验日
    二分查找的边界问题
    线段覆盖5
  • 原文地址:https://www.cnblogs.com/libaoting/p/javaThread.html
Copyright © 2011-2022 走看看