Chinaunix首页 | 论坛 | 博客
  • 博客访问: 522392
  • 博文数量: 151
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1405
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 14:32
文章分类

全部博文(151)

文章存档

2011年(1)

2010年(23)

2009年(1)

2008年(126)

我的朋友

分类: Java

2008-05-09 14:56:46

Java中的线程包括前台线程(用户线程)后台线程(守护线程)。当Java程序中只剩下后台线程运行时,整个Java程序随即变退出。相反,但还有前台线程在运行前,整个Java程序仍保持运行。

当线程产生而开始进行的时候,默认是作为前台线程运行的。让线程成为后台线程的方法是:在Thread对象调用start()方法前,先调用Thread对象的setDaemon(true)方法。其中若参数加false则让其成为前台线程之意。

一、创建线程的两种方法
    1、继承Thread类并覆盖Thread类的run()方法,要执行的代码写入子类的run()方法体内。如:
class MyThread extends Thread {
    public void run() {
        //要执行的代码
    }
}

然后创建MyThread的一个对象,并调用它的start()方法(从父类Thread继承而得来的):
Thread t = new MyThread();
t.setDaemon(false); //若想让其成为后台进程则传递true
t.start();

    2、Thread(Runnable target) 创建Thread对象时,给Thread构造函数传递一个实现Runnable接口的类。
    Thread(Runnable target)
Runnalbe接口只有一个方法,即run()方法。通过new Thread(Runnable target) 创建的Thread对象start()时调用的对象参数对应的run(),而不是Thread本身默认的run()。

具体做法如下:
class MyThread implements Runnable {
    public void run() {
        //要执行的代码
    }
}

...
Thread t = new Thread(new MyThread());
t.start();

二、两种线程的例子
   
举个例子:比如有4个售票窗口要共同卖10张票:
   1、采用Runnable接口方法
    class Aa
    {
     public static void main(String[] args)
     {
      TestThread tt = new TestThread();
      new Thread(tt).start();
      new Thread(tt).start();
      new Thread(tt).start();
      new Thread(tt).start();
     }
    }
   
    class TestThread implements Runnable
    {
     int tickets = 10;
   
     public void run()
     {
      while (tickets > 0)
      {
       System.out.println("TestThread: "
         + Thread.currentThread().getName() + "the tickets = "
         + tickets--);
      }
     }
    }
   
    执行结果:
    TestThread: Thread-0the tickets = 10
    TestThread: Thread-1the tickets = 9
    TestThread: Thread-1the tickets = 8
    TestThread: Thread-1the tickets = 7
    TestThread: Thread-1the tickets = 6
    TestThread: Thread-1the tickets = 5
    TestThread: Thread-0the tickets = 4
    TestThread: Thread-1the tickets = 3
    TestThread: Thread-0the tickets = 2
    TestThread: Thread-2the tickets = 1
    就相当于采用了线程同步
   
    2、采用继承Thread类的方法
    class Aa
    {
     public static void main(String[] args)
     {
      TestThread tt1 = new TestThread();
      new Thread(tt1).start();
      new Thread(tt1).start();
      new Thread(tt1).start();
      new Thread(tt1).start();
     }
    }
   
    class TestThread extends Thread
    {
     int tickets = 10;
   
     public void run()
     {
      while (tickets > 0)
      {
       System.out.println("TestThread: "
         + Thread.currentThread().getName() + "the tickets = "
         + tickets--);
      }
     }
    }          
    执行结果:
    TestThread: Thread-1the tickets = 10
    TestThread: Thread-3the tickets = 9
    TestThread: Thread-1the tickets = 8
    TestThread: Thread-1the tickets = 7
    TestThread: Thread-1the tickets = 5
    TestThread: Thread-1the tickets = 3
    TestThread: Thread-1the tickets = 1
    TestThread: Thread-3the tickets = 6
    TestThread: Thread-4the tickets = 2
    TestThread: Thread-2the tickets = 4
       

三、联合线程 
    t.join()的作用是将线程t合并到调用join()方法的当前线程中。说白了就是,t线程的代码不执行完,当前线程中的代码就只能一直等待。
    join()方法可带一表示合并时间的参数N,单位为毫秒。意思是N毫秒之后又恢复到合并前的状态。

阅读(988) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~