码迷,mamicode.com
首页 > 编程语言 > 详细

创建线程的多种方式

时间:2019-02-10 19:01:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:执行   dem   cut   values   task   todo   int   线程   extends   

技术图片

 1 public class Demo1 extends Thread{
 2     
 3     @Override
 4     public void run() {
 5         while(!interrupted()) {
 6             System.out.println(getName()+"线程执行了");
 7             try {
 8                 Thread.sleep(200);
 9             } catch (InterruptedException e) {
10                 // TODO Auto-generated catch block
11                 e.printStackTrace();
12             }
13             
14         }
15     }
16     
17     public static void main(String[] args) {
18         Demo1 d1 = new Demo1();
19         Demo1 d2 = new Demo1();
20         
21         d1.start();
22         d2.start();
23         
24         d1.interrupt();
25         
26     }
27     
28 
29 }

 

 技术图片

 1 public class Demo2 implements Runnable {
 2 
 3     @Override
 4     public void run() {
 5         while(true) {
 6             System.out.println("thread running ...");
 7         }
 8         
 9     }
10     
11     
12     public static void main(String[] args) {
13         Thread thread = new Thread(new Demo2());
14         thread.start();
15     }
16     
17 
18 }

技术图片

public class Demo3 {
    
    public static void main(String[] args) {
        
      //继承thread类子类方式
      /*new Thread() {
            public void run() {
                System.out.println("thread start ...");
            };
        }.start();
        */
        
        //实现runnable接口
/*        new Thread(new Runnable() {
            public void run() {
                System.out.println("thread start ...");
            }
        }).start();*/
        
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println("runnable");
                
            }
            
        }) {
            @Override
            public void run() {
            System.out.println("sub");
                
            }
        }.start();
        
        //sub 重写了父类的run方法      
    }

 技术图片

 1 public class Demo4 implements Callable<Integer>{ //指定返回类型
 2     
 3     public static void main(String[] args) throws Exception {
 4         Demo4 d = new Demo4();
 5         //class FutureTask<V> implements Runnables RunnableFuture<V>
 6             //   --- interface RunnableFuture<V> extends Runnable,Future<V> 对线程任务进行封装
 7         
 8         FutureTask<Integer> task = new FutureTask<>(d);
 9         
10         Thread t = new Thread(task);
11         t.start();
12         
13         Integer result = task.get();
14         System.out.println("线程执行结果为:"+result);
15     }
16 
17     @Override
18     public Integer call() throws  Exception {
19         System.out.println("正在进行紧张计算");
20         Thread.sleep(3000);
21         return 1;
22     }
23 
24 }

技术图片

 1 public class Demo5 {
 2     
 3     
 4     public static void main(String[] args) {
 5         
 6         Timer timer = new Timer();
 7         
 8         // abstract class TimerTask implements Runnable 
 9         timer.schedule(new TimerTask() {
10             
11             @Override
12             public void run() {
13                 //实现定时任务
14                 System.out.println("timertask is running");
15             }
16         }, 0, 1000);
17         //java.util.Timer.schedule(TimerTask task, long delay, long period)
18         
19     }
20 
21 }

技术图片

 1 public class Demo6 {
 2     
 3     public static void main(String[] args) {
 4         
 5         Executor threadPool = Executors.newFixedThreadPool(10);//固定容量的线程池
 6         
 7         for(int i = 0;i<10; i++ ) {
 8             threadPool.execute(new Runnable() {
 9 
10                 @Override
11                 public void run() {
12                     System.out.println(Thread.currentThread().getName());
13                 }
14             });
15         }
16     }
17 }

技术图片

 1 public class Demo7 {
 2     
 3     public static void main(String[] args) {
 4         List<Integer> values = Arrays.asList(10,20,30,40);  //  Arrays.asList(array):将数组array转化为List
 5         int res = new Demo7().add(values);
 6         System.out.println("计算结果为:"+res);
 7     }
 8     public int add(List<Integer> values) {
 9         values.parallelStream().forEach(System.out :: println);
10         return 0;
11         
12         //        30
13         //        20
14         //        10
15         //        40
16         //        计算结果为:0
17         //parallelStream 并行执行
18         // return values.parallelStream().mapToInt(a -> a).sum();
19         
20     }
21 
22 }

 

 技术图片

新建spring boot工程,pom 中引入spring-context依赖

//Config.java

@Configuration
@ComponentScan("com.roocon.thread.t1")
@EnableAsync
public class Config {

}

 

//DemoService

@Service public class DemoService { @Async public void a() { while(true) { System.out.println("a"); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Async public void b() { while(true) { System.out.println("b"); try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }

 

public class Main {
    
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
         DemoService ds = ac.getBean(DemoService.class);
         
         ds.a();
         ds.b();
        
    }

}

 

创建线程的多种方式

标签:执行   dem   cut   values   task   todo   int   线程   extends   

原文地址:https://www.cnblogs.com/quyangyang/p/10359954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!