好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java利用线程工厂监控线程池的实现示例

threadfactory

线程池中的线程从哪里来呢?就是threadfoctory

?

1

2

3

public interface threadfactory {

     thread newthread(runnable r);

}

threadfactory里面有个接口,当线程池中需要创建线程就会调用该方法,也可以自定义线程工厂

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

public class threadfactorytext {

     public static void main(string[] args) {

         runnable runnable= new runnable() {

             @override

             public void run() {

                 int num= new random().nextint( 10 );

                 system.out.println(thread.currentthread().getid()+ "--" +system.currenttimemillis()+ "--睡眠" +num);

                 try {

                     timeunit.seconds.sleep(num);

                 } catch (interruptedexception e) {

                     e.printstacktrace();

                 }

             }

         };

         //创建线程池 使用自定义线程工厂 采用默认的拒绝策略

         executorservice executorservice= new threadpoolexecutor( 5 , 5 , 0 , timeunit.seconds, new synchronousqueue<>(), new threadfactory() {

             @override

             public thread newthread(runnable r) {

                 thread t= new thread(r);

                 t.setdaemon( true ); //设置为守护线程,当主线程运行结束,线程池中线程也会被释放

                 system.out.println( "创建了线程" +t);

                 return t;

             }

         });

         //提交五个任务

         for ( int i = 0 ; i < 5 ; i++) {

             executorservice.submit(runnable);

         }

     }

}

当线程提交超过五个任务时,线程池会默认抛出异常

监控线程池

threadpoolexcutor提供了一组方法用于监控线程池

?

1

2

3

4

5

6

7

8

int getactivecount() //获得线程池只当前的获得线程数量

long getcompletedtaskcount() //返回线程池完成任务数量

int getcorepoolsize() //线程池中核心任务数量

int getlargestpoolsize() //返回线程池中曾经达到线程的最大数

int getmaximumpoolsize() //返回线程池的最大容量

int getpoolsize() //返回线程大小

blockingqueue<runnable> getqueue() //返回阻塞队列

long gettaskcount() //返回线程池收到任务总数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

public class text {

     public static void main(string[] args) throws interruptedexception {

         runnable runnable = new runnable() {

             @override

             public void run() {

                 system.out.println(thread.currentthread().getid() + "线程开始执行--" + system.currenttimemillis());

                 try {

                     thread.sleep( 10000 );

                 } catch (interruptedexception e) {

                     e.printstacktrace();

                 }

             }

         };

         //创建线程池 使用默认线程工厂 有界队列  采用discardpolicy策略

         threadpoolexecutor executorservice = new threadpoolexecutor( 2 , 5 , 0 , timeunit.seconds, new arrayblockingqueue<>( 5 ),executors.defaultthreadfactory(), new threadpoolexecutor.discardpolicy());

         //提交五个任务

         for ( int i = 0 ; i < 30 ; i++) {

             executorservice.submit(runnable);

             system.out.println( "当前线程核心线程数" +executorservice.getcorepoolsize()+ ",最大线程数:" +executorservice.getmaximumpoolsize()+ ",当前线程池大小:" +executorservice.getpoolsize()+ "活动线程数:" +executorservice.getactivecount()+ ",收到任务:" +executorservice.gettaskcount()+ "完成任务数:" +executorservice.getcompletedtaskcount()+ "等待任务数:" +executorservice.getqueue().size());

             timeunit.milliseconds.sleep( 500 );

         }

         system.out.println( "-------------------" );

         while (executorservice.getactivecount()>= 0 ) //继续对线程池进行检测

         {

           system.out.println( "当前线程核心线程数" +executorservice.getcorepoolsize()+ ",最大线程数:" +executorservice.getmaximumpoolsize()+ ",当前线程池大小:" +executorservice.getpoolsize()+ "活动线程数:" +executorservice.getactivecount()+ ",收到任务:" +executorservice.gettaskcount()+ "完成任务数:" +executorservice.getcompletedtaskcount()+ "等待任务数:" +executorservice.getqueue().size());

             thread.sleep( 1000 ); //每1秒检测一次

         }

 

     }

}

当线程池大小达到了核心线程数,线程会被放在等待队列。当线程池等待队列已满会开启新的线程。当当前线程大小达到最大线程数,等待队列也满了,再提交的话会执行discardpolicy策略,直接丢弃这个无法处理的任务,最后30个任务只剩下15个了。

原理如图:

扩展线程池

有时候需要对线程池进行扩展,如在监控每个任务开始和结束时间,或者自定义其他增强功能。

threadpoolexecutor线程池提供了两个方法:

?

1

2

protected void beforeexecute(thread t, runnable r) { }

protected void afterexecute(runnable r, throwable t) { }

线程池执行某个任务前会执行beforeexecute()方法,执行后会调用afterexecute()方法

查看threadpoolexecutor源码,在该类中定义了一个内部类worker,threadpoolexecutor线程池的工作线程就是worker类的实例,worker实例在执行时会调用beforeexecute与afterexecute方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public void run() {

             runworker( this );

}

final void runworker(worker w) {

                 try {

                     beforeexecute(wt, task);

                     try {

                         task.run();

                         afterexecute(task, null );

                     } catch (throwable ex) {

                         afterexecute(task, ex);

                         throw ex;

                     }

                 } finally {

                     task = null ;

                     w测试数据pletedtasks++;

                     w.unlock();

                 }

             }

     }

部分代码已省略,线程执行前会调用beforeexecute,执行后会调用afterexecute方法。

扩展线程池示例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

package com;

 

import java.util.concurrent.executorservice;

import java.util.concurrent.linkedblockingdeque;

import java.util.concurrent.threadpoolexecutor;

import java.util.concurrent.timeunit;

 

public class text07 {

     public static void main(string[] args) {

 

         //定义扩展线程池 定义线程池类继承threadpoolexecutor,然后重写其他方法

         executorservice threadpoolexecutor=

  new threadpoolexecutor( 5 , 5 , 0 , timeunit.seconds, new linkedblockingdeque<>()){

      //在内部类重写开始方法

      @override

      protected void beforeexecute(thread t, runnable r) {

          system.out.println(t.getid()+ "线程准备执行任务" +((mytask)r).name);

      }

      //在内部类重写结束方法

      @override

      protected void afterexecute(runnable r, throwable t) {

          system.out.println(((mytask)r).name+ "执行完成" );

      }

      //线程池退出

      @override

      protected void terminated() {

          system.out.println( "线程池退出" );

      }

  };

         for ( int i = 0 ; i < 5 ; i++) {

             mytask mytask= new mytask( "thread" +i);

             threadpoolexecutor.execute(mytask);

         }

     }

     private   static   class   mytask implements runnable

     {

         private   string name;

 

         public   mytask(string name)

         {

             this .name=name;

         }

         @override

         public void run() {

             system.out.println(name+ "正在被执行" +thread.currentthread().getid());

             try {

                 thread.sleep( 1000 ); //模拟任务时长

             } catch (interruptedexception e) {

                 e.printstacktrace();

             }

         }

     }

}

优化线程池大小

线程池大小对系统性能有一定影响,过大或者过小都无法方法发挥系统最佳性能,不需要非常精确,只要避免极大或者极小就可以了,一般来说线程池大小大姚考虑cpu数量

线程池大小=cpu数量 * 目标cpu使用率*(1+等待时间与计算时间的比)

线程池死锁

如果线程池执行中,任务a在执行过程中提交了任务b,任务b添加到线程池中的等待队列,如果a的结束需要b的执行结果,而b线程需要等待a线程执行完毕,就可能会使其他所有工作线程都处于等待状态,待这些任务在阻塞队列中执行。线程池中没有可以对阻塞队列进行处理的线程,就会一直等待下去照成死锁。

适合给线程池提交相互独立的任务,而不是彼此依赖的任务,对于彼此依赖的任务,可以考虑分别提交给不同的线程池来处理。

线程池异常信息捕获

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

import java.util.concurrent.executorservice;

import java.util.concurrent.synchronousqueue;

import java.util.concurrent.threadpoolexecutor;

import java.util.concurrent.timeunit;

 

public class text09 {

     public static void main(string[] args) {

         //创建线程池

         executorservice executorservice= new threadpoolexecutor( 5 , 5 , 0 , timeunit.seconds, new synchronousqueue<>());

         //向线程池中添加两个数相处计算的任务

         for ( int i = 0 ; i < 5 ; i++) {

             executorservice.submit( new text( 10 ,i));

         }

 

     }

     private   static class   text implements   runnable

     {

         private   int x;

         private   int y;

         public   text( int x, int y)

         {

             this .x=x;

             this .y=y;

         }

         @override

         public void run() {

             system.out.println(thread.currentthread().getname()+ "线程x/y结果的为" +x+ "/" +y+ "=" +(x/y));

         }

     }

}

可以看到只有四条结果,实际向线程池提交了五个任务,但是当i==0时,产生了算术异常,线程池把该异常吃掉了,导致我们对该异常一无所知

解决办法:

1.把submit改为execute

2.对线程池进行扩展,对submit进行包装

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

package com;

 

import java.util.concurrent.*;

 

public class text09 {

     public static void main(string[] args) {

         //创建线程池  使用自定义的线程池

         executorservice executorservice= new trancethreadpoorexcuter( 5 , 5 , 0 , timeunit.seconds, new synchronousqueue<>());

         //向线程池中添加两个数相处计算的任务

         for ( int i = 0 ; i < 5 ; i++) {

             executorservice.submit( new text( 10 ,i));

         }

 

     }

     public   static class   text implements   runnable

     {

         public   int x;

         public   int y;

         public   text( int x, int y)

         {

             this .x=x;

             this .y=y;

         }

 

         @override

         public void run() {

             system.out.println(thread.currentthread().getname()+ "线程x/y结果的为" +x+ "/" +y+ "=" +(x/y));

         }

     }

     //自定义线程池类 对trancethreadpoorexcuter进行扩展

     private   static   class   trancethreadpoorexcuter extends   threadpoolexecutor

     {

 

         public trancethreadpoorexcuter( int corepoolsize, int maximumpoolsize, long keepalivetime, timeunit unit, blockingqueue<runnable> workqueue) {

             super (corepoolsize, maximumpoolsize, keepalivetime, unit, workqueue);

         }

         //定义一个方法用于传入两个参数 第一个是要接受的任务 第二个是exception

         public   runnable warp(runnable r,exception e)

         {

             return new runnable() {

                 @override

                 public void run() {

 

                     try {

                         r.run();

                     }

                     catch (exception e1)

                     {

                         e.printstacktrace();

                         throw e1;

                     }

                 }

             };

         }

         //重写submit方法

         @override

         public future<?> submit(runnable task) {

             return super .submit(warp(task, new exception( "客户跟踪异常" )));

         }

         //还可以重写excute方法

     }

}

此方法使用了自定义的线程池,重写线程池中的submit方法,在submit方法中,把要传入的任务参数带一个捕获异常信息的功能就可以捕获线程池异常。

到此这篇关于java利用线程工厂监控线程池的实现示例的文章就介绍到这了,更多相关java 线程工厂监控线程池内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestcnblogs测试数据/cg-ww/p/14640908.html

查看更多关于Java利用线程工厂监控线程池的实现示例的详细内容...

  阅读:12次