小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 Java系列教程 查看内容

Java 多线程编程

发布者: 小白教程



实例

如下的ThreadClassDemo 程序演示了Thread类的一些方法:

  1. // 文件名 : DisplayMessage.java
  2. // 通过实现 Runnable 接口创建线程
  3. public class DisplayMessage implements Runnable
  4. {
  5.    private String message;
  6.    public DisplayMessage(String message)
  7.    {
  8.       this.message = message;
  9.    }
  10.    public void run()
  11.    {
  12.       while(true)
  13.       {
  14.          System.out.println(message);
  15.       }
  16.    }
  17. }

GuessANumber.java 文件代码:

  1. // 文件名 : GuessANumber.java
  2. // 通过继承 Thread 类创建线程
  3. public class GuessANumber extends Thread
  4. {
  5.    private int number;
  6.    public GuessANumber(int number)
  7.    {
  8.       this.number = number;
  9.    }
  10.    public void run()
  11.    {
  12.       int counter = 0;
  13.       int guess = 0;
  14.       do
  15.       {
  16.           guess = (int) (Math.random() * 100 + 1);
  17.           System.out.println(this.getName()
  18.                        + " guesses " + guess);
  19.           counter++;
  20.       }while(guess != number);
  21.       System.out.println("** Correct! " + this.getName()
  22.                        + " in " + counter + " guesses.**");
  23.    }
  24. }

ThreadClassDemo.java 文件代码:

  1. // 文件名 : ThreadClassDemo.java
  2. public class ThreadClassDemo
  3. {
  4.    public static void main(String [] args)
  5.    {
  6.       Runnable hello = new DisplayMessage("Hello");
  7.       Thread thread1 = new Thread(hello);
  8.       thread1.setDaemon(true);
  9.       thread1.setName("hello");
  10.       System.out.println("Starting hello thread...");
  11.       thread1.start();
  12.      
  13.       Runnable bye = new DisplayMessage("Goodbye");
  14.       Thread thread2 = new Thread(bye);
  15.       thread2.setPriority(Thread.MIN_PRIORITY);
  16.       thread2.setDaemon(true);
  17.       System.out.println("Starting goodbye thread...");
  18.       thread2.start();
  19.  
  20.       System.out.println("Starting thread3...");
  21.       Thread thread3 = new GuessANumber(27);
  22.       thread3.start();
  23.       try
  24.       {
  25.          thread3.join();
  26.       }catch(InterruptedException e)
  27.       {
  28.          System.out.println("Thread interrupted.");
  29.       }
  30.       System.out.println("Starting thread4...");
  31.       Thread thread4 = new GuessANumber(75);
  32.      
  33.            thread4.start();
  34.       System.out.println("main() is ending...");
  35.    }
  36. }

运行结果如下,每一次运行的结果都不一样。

  1. Starting hello thread...
  2. Starting goodbye thread...
  3. Hello
  4. Hello
  5. Hello
  6. Hello
  7. Hello
  8. Hello
  9. Hello
  10. Hello
  11. Hello
  12. Starting thread3...
  13. Hello
  14. Hello
  15. Starting thread4...
  16. Hello
  17. Hello
  18. main() is ending...

通过 Callable 和 Future 创建线程

  • 1. 创建 Callable 接口的实现类,并实现 call() 方法,该 call() 方法将作为线程执行体,并且有返回值。
  • 2. 创建 Callable 实现类的实例,使用 FutureTask 类来包装 Callable 对象,该 FutureTask 对象封装了该 Callable 对象的 call() 方法的返回值。
  • 3. 使用 FutureTask 对象作为 Thread 对象的 target 创建并启动新线程。
  • 4. 调用 FutureTask 对象的 get() 方法来获得子线程执行结束后的返回值。

实例

  1. public class CallableThreadTest implements Callable<Integer> {
  2. public static void main(String[] args)
  3. {
  4. CallableThreadTest ctt = new CallableThreadTest();
  5. FutureTask<Integer> ft = new
上一篇:Java 发送邮件下一篇:Java Applet基础

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 06:36 , Processed in 0.020840 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部