zoukankan      html  css  js  c++  java
  • Thread--currentThread()

    参考:http://bbs.csdn.net/topics/391872079

     1 package thread.demo01;
     2 
     3 public class MyThread extends Thread {
     4 
     5     public MyThread() {
     6         System.out.println("构造方法的打印 : " + Thread.currentThread().getName());
     7     }
     8     
     9     @Override
    10     public void run() {
    11         System.out.println("run方法的打印: " + Thread.currentThread().getName());
    12     }
    13 }
     1 package thread.demo01;
     2 
     3 public class Run2 {
     4 
     5     public static void main(String[] args) {
     6         MyThread myThread = new MyThread();
     7         myThread.start();
     8     }
     9     
    10 }

    MyThread的构造方法是main现成调用的

    而run方法是被新开的线程调用

    所以输出结果是:

     1 package thread.demo01;
     2 
     3 public class Run2 {
     4 
     5     public static void main(String[] args) {
     6         MyThread myThread = new MyThread();
     7 //        myThread.start();
     8         myThread.run();
     9     }
    10     
    11 }

    若更改代码为如上,run方法则是main线程调用,所以输出结果:

    ----------------------------------------------------------------------------------

     1 package thread.demo01;
     2 
     3 public class CurrentOperate extends Thread {
     4 
     5     public CurrentOperate() {
     6         System.out.println("CountOperate -- begin");
     7         System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
     8         System.out.println("this.getName() = " + this.getName());
     9         System.out.println("CountOperate -- end");
    10     }
    11     
    12     @Override
    13     public void run() {
    14         System.out.println("run -- begin");
    15         System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
    16         System.out.println("this.getName() = " + this.getName());
    17         System.out.println("run -- end");
    18     }
    19     
    20 }
     1 package thread.demo01;
     2 
     3 public class Run {
     4     
     5     public static void main(String[] args) {
     6         CurrentOperate c = new CurrentOperate();
     7         Thread c1 = new Thread(c);
     8         c1.start();
     9     }
    10 
    11 }

    和上面一样,这边的有个 Thread-1,是因为 CurrentOperate 传给了 Thread,所以

    调用run方法 不是main而是c1所在的线程。

    Thread.currentThread().getName()是获得调用这个方法的线程的名字,在main线程中调用的当然是main了。

    而this.getName()这个方法是获取当前对象的名字,只是单纯的方法的调用。

    ----------------------------------------------------------------------------------------------

     1 package thread.demo02;
     2 
     3 public class CountOperate extends Thread {
     4     
     5     public CountOperate() {
     6         System.out.println("CountOpreate -- begin");
     7         System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
     8         System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
     9         System.out.println("this.getName() = " + this.getName());
    10         System.out.println("this.isAlive() = " + this.isAlive());
    11         System.out.println("CountOpreate -- end");
    12     }
    13     
    14     @Override
    15     public void run() {
    16         System.out.println("run -- begin");
    17         System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
    18         System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
    19         System.out.println("this.getName() = " + this.getName());
    20         System.out.println("this.isAlive() = " + this.isAlive());
    21         System.out.println("run -- end");
    22     }
    23 
    24 }
     1 package thread.demo02;
     2 
     3 public class Run {
     4     
     5     public static void main(String[] args) {
     6         CountOperate c = new CountOperate();
     7         Thread t1 = new Thread(c);
     8         System.out.println("main begin t1 isAlive = " + t1.isAlive());
     9 //        t1.setName("A");
    10         t1.start();
    11         System.out.println("main end t1 isAlive = " + t1.isAlive());
    12     }
    13 
    14 }

  • 相关阅读:
    bzoj:1299: [LLH邀请赛]巧克力棒
    [51nod][cf468D]1558 树中的配对
    HDU5447 Good Numbers
    Timus Online Judge:ural:1006. Square Frames
    poj1830:开关问题
    bzoj:1776: [Usaco2010 Hol]cowpol 奶牛政坛
    bzoj:1725: [Usaco2006 Nov]Corn Fields牧场的安排
    bzoj:1828: [Usaco2010 Mar]balloc 农场分配
    bzoj:1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
    bzoj:1598: [Usaco2008 Mar]牛跑步
  • 原文地址:https://www.cnblogs.com/microcat/p/6321830.html
Copyright © 2011-2022 走看看