JAVA 多线程和并发学习笔记(三)

Java并发编程中使用Executors类建立和管理线程的用法html

 1.类 Executors
  Executors类能够看作一个“工具类”。援引JDK1.6 API中的介绍:java

     此包中所定义的 Executor、ExecutorService、ScheduledExecutorService、ThreadFactory 和 Callable 类的工厂和实用方法。此类支持如下各类方法:编程

  • 建立并返回设置有经常使用配置字符串的 ExecutorService 的方法。
  • 建立并返回设置有经常使用配置字符串的 ScheduledExecutorService 的方法。
  • 建立并返回“包装的”ExecutorService 方法,它经过使特定于实现的方法不可访问来禁用从新配置。
  • 建立并返回 ThreadFactory 的方法,它可将新建立的线程设置为已知的状态。
  • 建立并返回非闭包形式的 Callable 的方法,这样可将其用于须要 Callable 的执行方法中。

      经过这个类可以得到多种线程池的实例,例如能够调用newSingleThreadExecutor()得到单线程的ExecutorService,调 用newFixedThreadPool()得到固定大小线程池的ExecutorService,等等。拿到ExecutorService能够作的事情就比 较多了,最简单的是用它来执行Runnable对象,也能够执行一些实现了Callable<T>的对象。用Thread的start()方 法没有返回值,若是该线程执行的方法有返回值那用ExecutorService就再好不过了,能够选择submit()、invokeAll()或者 invokeAny(),根据具体状况选择合适的方法便可。此类中提供的一些方法有:多线程

1.1 public static ExecutorService newCachedThreadPool()
  建立一个可根据须要建立新线程的线程池,可是在之前构造的线程可用时将重用它们。对于执行不少短时间异步任务的程序而言,这些线程池一般可提升程序性能。

  1.2 public static ExecutorService newFixedThreadPool(int nThreads)
  建立一个可重用固定线程数的线程池,以共享的无界队列方式来运行这些线程。

  1.3 public static ExecutorService newSingleThreadExecutor()
  建立一个使用单个 worker 线程的 Executor,以无界队列方式来运行该线程。闭包

 

 2. 接口 ThreadFactory  并发

根据须要建立新线程的对象。使用线程工厂就无需再手工编写对 new Thread 的调用了,从而容许应用程序使用特殊的线程子类、属性等等。此接口最简单的实现就是:异步

1
2
3
4
5
class  SimpleThreadFactory implements ThreadFactory {
   public  Thread newThread(Runnable r) {
    return  new  Thread(r);
   }
  }

  

3. 接口ExecutorService工具

  该接口提供了管理终止的方法。性能


4.建立标准线程池启动线程spa


  4.1 提供一个简单的实现Runnable接口的线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public  class  MyThread implements Runnable {
   private  int  count = 1, number;
   
   public  MyThread( int  num) {
     number = num;
     System. out .println( "Create Thread-"  + number);
   }
   
   public  void  run() {
     while  ( true ) {
       System. out .println( "Thread-"  + number +  " run "  + count+ " time(s)" );
       if  (++count == 3)
        return ;
     }
   }
}

  4.2使用CachedThreadPool启动线程

1
2
3
4
5
6
7
8
9
10
11
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
   
public  class  CachedThreadPool {
   public  static  void  main(String[] args) {
     ExecutorService exec = Executors.newCachedThreadPool();
     for  ( int  i = 0; i < 5; i++)
       exec.execute( new  MyThread(i));
     exec.shutdown();
   }
}

  

  4.3 使用FixedThreadPool启动线程

1
2
3
4
5
6
7
8
9
10
11
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
   
public  class  FixedThreadPool {
   public  static  void  main(String[] args) {
     ExecutorService exec = Executors.newFixedThreadPool(2);
     for  ( int  i = 0; i < 5; i++)
       exec.execute( new  MyThread(i));
     exec.shutdown();
   }
}

  

  4.4 使用SingleThreadExecutor启动线程

1
2
3
4
5
6
7
8
9
10
11
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
   
public  class  SingleThreadExecutor {
   public  static  void  main(String[] args) {
     ExecutorService exec = Executors.newSingleThreadExecutor();
     for  ( int  i = 0; i < 5; i++)
       exec.execute( new  MyThread(i));
     exec.shutdown();
   }
}

  

  5.配合ThreadFactory接口的使用


   给线程加入daemon和priority的属性设置 设置后台线程属性,设置优先级属性

1
2
3
4
5
6
7
8
9
10
import java.util.concurrent.ThreadFactory;
   
public  class  MaxPriorityThreadFactory implements ThreadFactory {
   public  Thread newThread(Runnable r) {
     Thread t =  new  Thread(r);
     t.setPriority(Thread.MAX_PRIORITY);
     return  t;
   }
}

  

1
2
3
4
5
6
7
8
9
import java.util.concurrent.ThreadFactory;
   
public  class  DaemonThreadFactory implements ThreadFactory {
   public  Thread newThread(Runnable r) {
     Thread t =  new  Thread(r);
     t.setDaemon( true );
     return  t;
   }
}

  

1
2
3
4
5
6
7
8
9
10
//最低优先级
import java.util.concurrent.ThreadFactory;
   
public  class  MinPriorityThreadFactory implements ThreadFactory {
   public  Thread newThread(Runnable r) {
     Thread t =  new  Thread(r);
     t.setPriority(Thread.MIN_PRIORITY);
     return  t;
   }
}

  5.3启动带有属性设置的线程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
   
public  class  ExecFromFactory {
   public  static  void  main(String[] args) throws Exception {
     ExecutorService defaultExec = Executors.newCachedThreadPool();
     ExecutorService daemonExec = Executors
        .newCachedThreadPool( new  DaemonThreadFactory());
     ExecutorService maxPriorityExec = Executors
        .newCachedThreadPool( new  MaxPriorityThreadFactory());
     ExecutorService minPriorityExec = Executors
        .newCachedThreadPool( new  MinPriorityThreadFactory());
     for  ( int  i = 0; i < 10; i++)
       daemonExec.execute( new  MyThread(i));
     for  ( int  i = 10; i < 20; i++)
       if  (i == 10)
        maxPriorityExec.execute( new  MyThread(i));
       else  if  (i == 11)
        minPriorityExec.execute( new  MyThread(i));
       else
        defaultExec.execute( new  MyThread(i));
   }
}

  

参考文章:

1. Java多线程与并发

2. Java 线程池的使用

相关文章
相关标签/搜索