Java1.5 ( 5.0 )新功能:线程调度 : 简单的线程 « 线程 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 线程 » 简单的线程屏幕截图 
Java1.5 ( 5.0 )新功能:线程调度
Java1.5 ( 5.0 )新功能:线程调度
 

/*
License for Java 1.5 'Tiger': A Developer's Notebook
     (O'Reilly) example package

Java 1.5 'Tiger': A Developer's Notebook (O'Reilly) 
by Brett McLaughlin and David Flanagan.
ISBN: 0-596-00738-8

You can use the examples and the source code any way you want, but
please include a reference to where it comes from if you use it in
your own products or services. Also note that this software is
provided by the author "as is", with no expressed or implied warranties. 
In no event shall the author be liable for any direct or indirect
damages arising in any way out of the use of this software.
*/



import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

import static java.util.concurrent.TimeUnit.*;

public class ScheduleTester {

  public static void main(String[] args) {
    // Get the scheduler
    ScheduledExecutorService scheduler =
      Executors.newSingleThreadScheduledExecutor();

    // Get a handle, starting now, with a 10 second delay
    final ScheduledFuture<?> timeHandle =
      scheduler.scheduleAtFixedRate(new TimePrinter(System.out)010, SECONDS);    

    // Schedule the event, and run for 1 hour (60 * 60 seconds)
    scheduler.schedule(new Runnable() {
      public void run() {
        timeHandle.cancel(false);
      }
    }60*60, SECONDS);


    /**
     * On some platforms, you'll have to setup this infinite loop to see output
    while (true) { }
     */
  }
}

class TimePrinter implements Runnable {

  private PrintStream out;

  public TimePrinter(PrintStream out) {
    this.out = out;
  }

  public void run() {
    out.printf("Current time: %tr%n"new Date());
  }
}
           
         
  
Related examples in the same category
1. 通过实现接口创建一个线程
2. 通过继承创建一个线程
3. 线程提醒线程提醒
4. 线程抢占线程抢占
5. Suggesting when to switch threads with yield()Suggesting when to switch threads with yield()
6. 内部类创建线程内部类创建线程
7. 停止线程的安全方法
8. 等待一段时间调用睡眠方法等待一段时间调用睡眠方法
9. 演示加入方法演示加入方法
10. 守护线程产生其他守护线程守护线程产生其他守护线程
11. 守护线程不妨碍程序终止。
12. 显示使用线程优先级显示使用线程优先级
13. Java的新功能:多线程Java的新功能:多线程
14. 两个简单的线程两个简单的线程
15. Simple threads creator Simple threads creator
16. 任务
17. SimpleThread使用运行界面。SimpleThread使用运行界面。
18. 简单线程演示。简单线程演示。
19. 测试覆盖线程测试覆盖线程
20. 测试覆盖
21. 多处理器并行循环机
22. 显示所有正在运行的线程
23. 确定线程对象的优先次序
24. 线程的优先级。
25. 使用ThreadGroup 。
26. 三线程测试三线程测试
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.