• 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);//
        }
    
    }
  • 相关阅读:
    Iterator与 Enumeration
    多态性理解
    django---路由层
    django常用模块汇总
    django初识
    python常见模块
    python PEP8规范
    logging模块
    mysql一些了解的名词
    python 链接 mysql数据库
  • 原文地址:https://www.cnblogs.com/libaoting/p/javaThread.html
Copyright © 2020-2023  润新知