java多线程入门学习(二)

java多线程入门学习(二)

一.currentThread()和isAlive()

1. currentThread()方法

         返回的是当前运行线程,首先来个简单的例子了解一下这个方法的意义:
     
     
     
     
  1. package cn.sun.method;
  2. public class demo01 {
  3. public static void main(String[] args) {
  4. System.out.println(Thread.currentThread().getName());
  5. }
  6. }

         返回结果如下:
     
     
     
     
  1. main
         这样我们就知道当前运行的是main主函数。
         以下我们创建一个复杂一点的看下这里到底应该是哪个线程在某个时刻运行:
     
     
     
     
  1. package cn.sun.method;
  2. public class Demo02 extends Thread{
  3. public Demo02(){
  4. System.out.println("demo01 begin...");
  5. System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
  6. System.out.println("this.getName = "+ this.getName());
  7. System.out.println("demo02 end...");
  8. }
  9. public void run(){
  10. System.out.println("run begin...");
  11. System.out.println("Thread.currentThread().getName() = "+Thread.currentThread().getName());
  12. System.out.println("this.getName = "+ this.getName());
  13. System.out.println("run end...");
  14. }
  15. }
运行测试:
     
     
     
     
  1. package cn.sun.test;
  2. import cn.sun.method.Demo02;
  3. public class Test01 {
  4. public static void main(String[] args) {
  5. Demo02 demo = new Demo02();
  6. Thread thread = new Thread(demo);
  7. thread.setName("A");
  8. thread.start();
  9. }
  10. }
结果如下:
     
     
     
     
  1. demo01 begin...
  2. Thread.currentThread().getName() = main
  3. this.getName = Thread-0
  4. demo02 end...
  5. run begin...
  6. Thread.currentThread().getName() = A
  7. this.getName = Thread-0
  8. run end...

2. isAlive()方法

          判断当前线程是否处于活动状态
首先来个例子看一看怎么回事:
     
     
     
     
  1. package cn.sun.method;
  2. public class MyThread extends Thread{
  3. @Override
  4. public void run() {
  5. System.out.println("run = "+this.isAlive());
  6. }
  7. }
测试类:
     
     
     
     
  1. package cn.sun.test;
  2. import cn.sun.method.Demo02;
  3. import cn.sun.method.MyThread;
  4. public class Test01 {
  5. public static void main(String[] args) {
  6. MyThread myThread = new MyThread();
  7. System.out.println("begin =="+myThread.isAlive());
  8. myThread.start();
  9. System.out.println("end =="+myThread.isAlive());
  10. }
  11. }
然后我们发现结果是这样子的:
     
     
     
     
  1. begin ==false
  2. end ==true
  3. run = true
总得来说这个方法不是太难,关键要分清楚某一时刻是不是出于活动状态,还有分清楚Thread.currentThread().getName()与this.getName()的区别。
最后来个例子,看例子比较清楚
     
     
     
     
  1. package cn.sun.method;
  2. public class CountOperate extends Thread{
  3. public CountOperate(){
  4. System.out.println("CountOperate begin...");
  5. System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
  6. System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
  7. System.out.println("this.getName()="+this.getName());
  8. System.out.println("this.isAlive()="+this.isAlive());
  9. System.out.println("CountOperate end...");
  10. }
  11. @Override
  12. public void run() {
  13. System.out.println("run begin...");
  14. System.out.println("Thread.currentThread().getName()="+Thread.currentThread().getName());
  15. System.out.println("Thread.currentThread().isAlive()="+Thread.currentThread().isAlive());
  16. System.out.println("this.getName()="+this.getName());
  17. System.out.println("this.isAlive()="+this.isAlive());
  18. System.out.println("run end...");
  19. }
  20. }
测试类:
      
      
      
      
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. public class Test01 {
  6. public static void main(String[] args) {
  7. CountOperate countOperate = new CountOperate();
  8. Thread thread = new Thread(countOperate);
  9. System.out.println("main begin threat isAlive = "+thread.isAlive());
  10. thread.setName("A");
  11. thread.start();
  12. System.out.println("main end thread isAlive = "+thread.isAlive());
  13. }
  14. }
结果如下:
      
      
      
      
  1. CountOperate begin...
  2. Thread.currentThread().getName()=main
  3. Thread.currentThread().isAlive()=true
  4. this.getName()=Thread-0
  5. this.isAlive()=false
  6. CountOperate end...
  7. main begin threat isAlive = false
  8. main end thread isAlive = true
  9. run begin...
  10. Thread.currentThread().getName()=A
  11. Thread.currentThread().isAlive()=true
  12. this.getName()=Thread-0
  13. this.isAlive()=false
  14. run end...

3.this和Thread.currentThread()区别

Thread.currentThread() 返回当前线程对象,也就是执行这句代码这个线程。
this,也是当前线程对象~~~~
如果线程是start()方法调用起来的,那么二者是相等的。
不过,run()方法有可能被直接调用,这种情况下就不相等了。
       
       
       
       
  1. new Thread("aaa"){
  2. public void run(){
  3. System.out.println(Thread.currentThread() == this);
  4. new Thread("bbb"){
  5. public void run(){
  6. System.out.println(Thread.currentThread() == this);
  7. }
  8. }.start();
  9. new Thread("ccc"){
  10. public void run(){
  11. System.out.println(Thread.currentThread() == this);
  12. }
  13. }.run();
  14. }
  15. }.start();
结果是:
      
      
      
      
  1. true
  2. true
  3. false

第3个ccc线程是用run()方法调用的,那么就相当于new了一个普通类,调用了它的一个方法,仅此而已。
Thread.currentThread()返回的是aaa线程,this是ccc对象,所以不相等。

二.sleep()和getId()

1.sleep()方法

         在指定的毫秒数内让当前正在执行的线程休眠,这里的当前线程就是this.currentThread()返回的线程。。
简单举个例子看一下:
    
    
    
    
  1. package cn.sun.method;
  2. public class SleepDemo extends Thread{
  3. @Override
  4. public void run() {
  5. try {
  6. System.out.println("run threadName "+this.currentThread().getName()+" begin "+System.currentTimeMillis());
  7. Thread.sleep(2000);
  8. System.out.println("run threadName "+this.currentThread().getName()+" end "+System.currentTimeMillis());
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. }
测试类:
     
     
     
     
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. import cn.sun.method.SleepDemo;
  6. public class Test01 {
  7. public static void main(String[] args) {
  8. SleepDemo s = new SleepDemo();
  9. System.out.println("begin="+System.currentTimeMillis());
  10. s.start();
  11. System.out.println("end="+System.currentTimeMillis());
  12. }
  13. }
结果是:
     
     
     
     
  1. begin=1460526284619
  2. end=1460526284619
  3. run threadName Thread-0 begin 1460526284620
  4. run threadName Thread-0 end 1460526286620

2. getId()方法

          便是线程唯一标示符
     
     
     
     
  1. package cn.sun.test;
  2. import cn.sun.method.CountOperate;
  3. import cn.sun.method.Demo02;
  4. import cn.sun.method.MyThread;
  5. import cn.sun.method.SleepDemo;
  6. public class Test01 {
  7. public static void main(String[] args) {
  8. Thread runThread = Thread.currentThread();
  9. System.out.println("name="+runThread.getName()+" id="+runThread.getId());
  10. }
  11. }
得到的是:
     
     
     
     
  1. name=main id=1



你可能感兴趣的:(java多线程入门学习(二))