免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1088 | 回复: 0
打印 上一主题 下一主题

java线程 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-09 14:56 |只看该作者 |倒序浏览

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();
二、两种线程创建方法的比较
   
使用Runnable接口适合多个相同程序代码的线程去处理同一资源的情况。
   举个例子:比如有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();
      tt1.start();
      TestThread tt2 = new TestThread();
      tt2.start();
      TestThread tt3 = new TestThread();
      tt3.start();
      TestThread tt4 = new TestThread();
      tt4.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-0the tickets = 10
    TestThread: Thread-0the tickets = 9
    TestThread: Thread-0the tickets = 8
    TestThread: Thread-0the tickets = 7
    TestThread: Thread-0the tickets = 6
    TestThread: Thread-0the tickets = 5
    TestThread: Thread-0the tickets = 4
    TestThread: Thread-0the tickets = 3
    TestThread: Thread-0the tickets = 2
    TestThread: Thread-1the tickets = 10
    TestThread: Thread-0the tickets = 1
    TestThread: Thread-2the tickets = 10
    TestThread: Thread-3the tickets = 10
    TestThread: Thread-1the tickets = 9
    TestThread: Thread-2the tickets = 9
    TestThread: Thread-3the tickets = 9
    TestThread: Thread-1the tickets = 8
    TestThread: Thread-2the tickets = 8
    TestThread: Thread-3the tickets = 8
    TestThread: Thread-1the tickets = 7
    TestThread: Thread-2the tickets = 7
    TestThread: Thread-3the tickets = 7
    TestThread: Thread-1the tickets = 6
    TestThread: Thread-2the tickets = 6
    TestThread: Thread-3the tickets = 6
    TestThread: Thread-1the tickets = 5
    TestThread: Thread-2the tickets = 5
    TestThread: Thread-1the tickets = 4
    TestThread: Thread-3the tickets = 5
    TestThread: Thread-2the tickets = 4
    TestThread: Thread-1the tickets = 3
    TestThread: Thread-3the tickets = 4
    TestThread: Thread-2the tickets = 3
    TestThread: Thread-3the tickets = 3
    TestThread: Thread-2the tickets = 2
    TestThread: Thread-1the tickets = 2
    TestThread: Thread-3the tickets = 2
    TestThread: Thread-2the tickets = 1
    TestThread: Thread-1the tickets = 1
    TestThread: Thread-3the tickets = 1
   
    可见用Runnable接口方式才能让4个售票点共同销售,而继承Thread类,4个线程的数据各自独立

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


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/67175/showart_682512.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP