「java得到当前线程」java获取当前线程

博主:adminadmin 2022-12-11 17:21:07 82

今天给各位分享java得到当前线程的知识,其中也会对java获取当前线程进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java中能否得到当前线程是由哪个对象创建来的

似乎只有在这个线程的构造方法或start() 方法中能够有机会记录快照,在一个创建被启动运行之后是没有机会知道的。这里面创建过程只能知道由哪个类的哪个方法在哪个代码行创建的,无法精确地知道是哪个对象实例创建的,除非我们明确的在线程的构造方法中传入一个创建者对象的实例。

如果想做成框架一般通用的自动化的填充创建者而不去修改现有代码的话,我们可能通过 AOP Introduction 特性做到这点,比如 AspectJ 就支持 Introduction 特性,也就是能给一个类增加特性,比如未实现 java.io.Serializable 的类增加 Serializable 接口,另外再添加个方法什么的。

例如:

public class Worker extends Thread {

   private Exception scenario;

   

   public Worker() {}

   

   public synchronized void start() {

      scenario = new Exception();

      super.start();

   }

   

   public void run() {

      System.out.println("我的启动过程如下:");

      printScenario();

   }

   public void printScenario() {

      scenario.printStackTrace(); // 打印快照,我们通过扫描 scenario.getStackTrace(); 来分析第一个 StackTraceElement 是哪个类和方法就知道了。

   }

}

java获得当前线程有两种方法,第一种是Thread.currentThread();谁知道另外一种?

另外一种是实现Runnable接口,implements Runnable

这种方法有两个好处是

(1)适合多个相同程序代码的线程去处理同一资源的情况,把虚拟CPU(线程)同程序的代码,数据有效的分离,较好地体现了面向对象的设计思想。

(2)可以避免由于Java的单继承特性带来的局限。经常碰到这样一种情况,即当要将已经继承了某一个类的子类放入多线程中,由于一个类不能同时有两个父类,所以不能用继承Thread类的方式,那么,这个类就只能采用实现Runnable接口的方式了。

java获取当前线程状态。

java线程的状态有下面几种状态:

/**

* Thread state for a thread which has not yet started.

*/

NEW,

/**

* Thread state for a runnable thread. A thread in the runnable

* state is executing in the Java virtual machine but it may

* be waiting for other resources from the operating system

* such as processor.

*/

RUNNABLE,

/**

* Thread state for a thread blocked waiting for a monitor lock.

* A thread in the blocked state is waiting for a monitor lock

* to enter a synchronized block/method or

* reenter a synchronized block/method after calling

* {@link Object#wait() Object.wait}.

*/

BLOCKED,

/**

* Thread state for a waiting thread.

* A thread is in the waiting state due to calling one of the

* following methods:

* ul

* li{@link Object#wait() Object.wait} with no timeout/li

* li{@link #join() Thread.join} with no timeout/li

* li{@link LockSupport#park() LockSupport.park}/li

* /ul

*

* pA thread in the waiting state is waiting for another thread to

* perform a particular action.

*

* For example, a thread that has called ttObject.wait()/tt

* on an object is waiting for another thread to call

* ttObject.notify()/tt or ttObject.notifyAll()/tt on

* that object. A thread that has called ttThread.join()/tt

* is waiting for a specified thread to terminate.

*/

WAITING,

/**

* Thread state for a waiting thread with a specified waiting time.

* A thread is in the timed waiting state due to calling one of

* the following methods with a specified positive waiting time:

* ul

* li{@link #sleep Thread.sleep}/li

* li{@link Object#wait(long) Object.wait} with timeout/li

* li{@link #join(long) Thread.join} with timeout/li

* li{@link LockSupport#parkNanos LockSupport.parkNanos}/li

* li{@link LockSupport#parkUntil LockSupport.parkUntil}/li

* /ul

*/

TIMED_WAITING,

/**

* Thread state for a terminated thread.

* The thread has completed execution.

*/

TERMINATED;

用c语言 java 来获取当前(进程)线程状态

通过调用Thread.getState()方法获取当前线程的状态。以下是我的代码,可以直接编译运行。

public class Test {

public static void main(String[] args) {

new NewThread().start(); //启动线程

}

}

class NewThread extends Thread{

public NewThread() {

super("NewThread"); //定义当前线程的名称为NewThread

}

@Override

public void run() {

System.out.println("当前线程:"+currentThread().getName()+"运行状态为:"+getState()); //打印线程的运行状态

}

}

java得到当前线程的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取当前线程、java得到当前线程的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-11,除非注明,否则均为首码项目网原创文章,转载请注明出处。