知识大全 Java单任务延迟相关代码的学习笔记

Posted

篇首语:智者不为愚者谋,勇者不为怯者死。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java单任务延迟相关代码的学习笔记相关的知识,希望对你有一定的参考价值。

Java单任务延迟相关代码的学习笔记  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  Java单任务延迟连接池在四代码基础上 做改动 这个就需要我们不选的学习 下面我们就看看如何才能更好的使用 希望我们在下面的使用中大家更了解相关的代码

  Java <>单任务延迟代码

  创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行

   ScheduledExecutorService pool = Executors newSingleThread

  ScheduledExecutor();

  创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行

   ScheduledExecutorService pool = Executors newSingle

  ThreadScheduledExecutor();

  Java代码

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   Process finished with exit code

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   Process finished with exit code

  自定义线程池

  Java代码

   import ncurrent ArrayBlockingQueue;

   import ncurrent BlockingQueue;

   import ncurrent ThreadPoolExecutor;

   import ncurrent TimeUnit;

   /**

   * Java线程 线程池 自定义线程池

   *

   * @author Administrator : :

   */

   public class Test

   public static void main(String[] args)

   //创建等待队列

   BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue

  <Runnable>( );

   //创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行

   ThreadPoolExecutor pool = new ThreadPoolExecutor

  ( TimeUnit MILLISECONDS bqueue);

   //创建实现了Runnable接口对象 Thread对象当然也实现了Runnable接口

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   //将线程放入池中进行执行

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   //关闭线程池

   pool shutdown();

  

  

   class MyThread extends Thread

   @Override

   public void run()

   System out println(Thread currentThread() getName() +

   正在执行 );

   try

   Thread sleep( L);

   catch (InterruptedException e)

   e printStackTrace();

  

  

  

   import ncurrent ArrayBlockingQueue;

   import ncurrent BlockingQueue;

   import ncurrent ThreadPoolExecutor;

   import ncurrent TimeUnit;

  

   /**

   * Java线程 线程池 自定义线程池

   *

   * @author Administrator : :

   */

   public class Test

   public static void main(String[] args)

   //创建等待队列

   BlockingQueue<Runnable> bqueue = new ArrayBlockingQueue

  <Runnable>( );

   //创建一个单线程执行程序 它可安排在给定延迟后运行命令或者定期地执行

   ThreadPoolExecutor pool = new ThreadPoolExecutor

  ( TimeUnit MILLISECONDS bqueue);

   //创建实现了Runnable接口对象 Thread对象当然也实现了Runnable接口

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   Thread t = new MyThread();

   //将线程放入池中进行执行

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   pool execute(t );

   //关闭线程池

   pool shutdown();

  

  

   class MyThread extends Thread

   @Override

   public void run()

   System out println(Thread currentThread() getName() +

   正在执行 );

   try

   Thread sleep( L);

   catch (InterruptedException e)

   e printStackTrace();

  

  

  

  Java代码

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   Process finished with exit code

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

   pool thread 正在执行

cha138/Article/program/Java/hx/201311/25732

相关参考