-
Thread.currentThread().getName 和 this.getName有什么区别?5
直接上代码吧import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; class UnCatchExceptionThread extends Thread{ public UnCatchExceptionThread(String name){ this.setName(name); } @Override public void run() { System.out.println("Thread name is: " + this.getName()); throw new RuntimeException(); } } class UnCatchExceptionHandler implements Thread.UncaughtExceptionHandler{ @Override public void uncaughtException(Thread t, Throwable e) { System.out.println("catch " + e + " from " + t.getName()); } } class HandlerFactory implements ThreadFactory{ @Override public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setUncaughtExceptionHandler(new UnCatchExceptionHandler()); return t; } } public class CaptureException { public int i; /** * @param args */ public static void main(String[] args) { ExecutorService exec = Executors.newCachedThreadPool(new HandlerFactory()); exec.execute(new UnCatchExceptionThread("Gemoji")); } }
输出如下:引用
Thread name is: Gemoji
catch java.lang.RuntimeException from Thread-1
而把System.out.println("Thread name is: " + this.getName());
换成System.out.println("Thread name is: " + Thread.currentThread().getName());
之后,输出就变成了:引用
Thread name is: Thread-1
catch java.lang.RuntimeException from Thread-1
这是为什么呢?2011年12月28日 21:56
目前还没有答案
相关推荐
System.out.println("Activity-->"+ Thread.currentThread().getName()); } Runnable r = new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { Thread...
System.out.println("Current thread name: " + Thread.currentThread().getName()); System.out.println("run() method called"); } } public class Main { public static void main(String[] args) { ...
System.out.println("线程" + Thread.currentThread().getName() + ":" + i); try { sleep((int) (Math.random() * 10)); } catch (InterruptedException e) { e.printStackTrace(); } } System.out....
System.out.println(Thread.currentThread().getName() + "-start"); // 创建并启动子线程 for (int i = 0; i ; i++) { new SubThread(runningThreadNum).start(); } // 等待所有子线程完成 ...
System.out.println(Thread.currentThread().getName() + ": is run"); } } } class NewThread implements Runnable { int index = 0; @Override public void run() { while (true) { // System.out....
System.out.println(Thread.currentThread().getName() + " start ta"); ta.start(); System.out.println(Thread.currentThread().getName() + " block"); ta.wait(); // 等待 System.out.println(Thread....
System.out.println("main(): "+Thread.currentThread().getName() + " is running"); } } } class TestThread extends Thread { public void run() { while(true) { System.out.println("TestThread: "+...
System.out.println(Thread.currentThread().getName() + " : " + i); if (i == 20) { new FirstThreadTest().start(); new FirstThreadTest().start(); } } } } ``` 通过 Runnable 接口创建线程类 通过 ...
System.out.println(Thread.currentThread().getName()+"-inc:"+j); } private synchronized void dec(){ j--; System.out.println(Thread.currentThread().getName()+"-dec:"+j); } class Inc ...
在这个“currentThread.getName.rar”压缩包中,我们主要探讨的是如何在Java中通过继承`Thread`类以及使用`currentThread().getName()`方法来跟踪和理解线程的状态。 首先,我们来看`Thread`类。在Java中,创建线程...
在这种情况下,可以使用`Thread.currentThread().getName()`获取当前线程的名称,并在输出语句中包含它,以便跟踪每个任务的状态。 例如: ```java public class PrintOrderExample { public static void main...
System.out.println("主线程:" + Thread.currentThread().getName()); MyThread thread = new MyThread(); thread.start(); } } class MyThread extends Thread { @Override public void run() { System....
System.out.println(Thread.currentThread().getName() + " synchronized loop " + i); } } } public static void main(String[] args) { Thread1 t1 = new Thread1(); Thread ta = new Thread(t1, "400电话...
System.out.println(Thread.currentThread().getName()); } } public class Maindemo { public static void main(String[] args) { ThreadAdemo ta = new ThreadAdemo(); // 调用 run(),线程不会启动,仍然在...
System.out.println(Thread.currentThread().getName()); } } ``` 在这个例子中,`SimpleThread`类继承了`Thread`,并且重写了`run()`方法。在`main`方法中,通过`start()`启动新线程并执行`run()`。 2. 实现...
System.out.println(Thread.currentThread().getName() + "请求获取" + name); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread()....
System.out.println(Thread.currentThread().getName()+"正在等待命令"); cdOrder.await();//子线程等待 System.out.println(Thread.currentThread().getName()+"接受命令"); Thread.sleep(2000); System.out....
System.out.println(Thread.currentThread().getName()+"线程出售的是第"+ num-- +"张火车票!"); } } public static void main(String[] args) { SaleTickets s = new SaleTickets(); //:窗口...
System.out.println("Task 1 completed by " + Thread.currentThread().getName()); } }; executor.execute(task1); Runnable task2 = new Runnable() { @Override public void run() { System.out.println...
System.out.println("子线程"+Thread.currentThread().getName()+"正在执行"); Thread.sleep(3000); System.out.println("子线程"+Thread.currentThread().getName()+"执行完毕"); latch.countDown(); } catch ...