ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

创建线程的多种方式

2019-02-10 18:50:53  阅读:184  来源: 互联网

标签:多种 创建 void System class 线程 println new public


 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();
        
    }

}

 

标签:多种,创建,void,System,class,线程,println,new,public
来源: https://www.cnblogs.com/quyangyang/p/10359954.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有