线程开启方式

方式一:

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Component//spring托管
public class TreadTest {
    protected ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    @PostConstruct//类托管执行构造后 执行该方法
    public void haha(){
         Runnable loop = () -> {
        System.out.println("======================"+Thread.currentThread().getName()+"  "+System.currentTimeMillis());
         };
        System.out.println("======================"+Thread.currentThread().getName()+"  "+System.currentTimeMillis());
        executor.scheduleWithFixedDelay(loop,50, 100, TimeUnit.MILLISECONDS);//参数1:runnable,2:执行的时间,3:执行间隔:4:时间单位
    }
}

方式二

    public static void  main(String arg[]){
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext*.xml");
        String param="myRunnable";

        Thread t = new Thread((Runnable) applicationContext.getBean(param));//创建开始并线程
        t.start();

        boolean loop = true;
        while (loop) {//主程序不断循环
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                loop = false;
            }
        }
        //fore(Thread t:threads)
            try {
                t.join();//调用者占用线程
            } catch (InterruptedException e) {
                System.out.println("等待线程结束异常");
        }
    }
@Component
public class MyRunnable implements Runnable {
    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {

            System.out.println(Thread.currentThread().getName()+"====="+System.currentTimeMillis());
            System.out.println(Thread.currentThread().isInterrupted());

            // 1分钟同步一次
           for (int i=0; i<1*60*100; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
    }
}   

方式三

@Component
public class ListenerTest implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        while (!Thread.currentThread().isInterrupted()) {

            System.out.println(Thread.currentThread().getName()+"====="+System.currentTimeMillis());
            System.out.println(Thread.currentThread().isInterrupted());

            // 1分钟同步一次
            for (int i=0; i<1*60*100; i++) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/jaycegg/article/details/80609152