线程的构造方法和TA的两种实现方式
程序员文章站
2022-06-08 16:30:31
...
1. 构造方法
1.1 示例程序
/**
* @className: ThreadConstructor
* @description: Thread Constructor
**/
public class ThreadConstructor {
public static void main(String[] args) {
init();
}
private static void init() {
// target runnable
final Runnable targetRunnable = new Runnable() {
@Override
public void run() {
// to do something
}
};
// mainThread's threadGroup; default group is currentThread's parent GroupThread
final ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
final String thread_04Name = "Thread-Constructor-04";
final String thread_05Name = "Thread-Constructor-05";
final String thread_06Name = "Thread-Constructor-06";
final String thread_07Name = "Thread-Constructor-07";
final String thread_08Name = "Thread-Constructor-08";
// init stack size, Beyond method stack throw java.lang.*Error
long stackSize = 1 << 24;
Thread thread_01 = new Thread();
Thread thread_02 = new Thread(targetRunnable);
Thread thread_03 = new Thread(mainThreadGroup, targetRunnable);
Thread thread_04 = new Thread(thread_04Name);
Thread thread_05 = new Thread(mainThreadGroup, thread_05Name);
Thread thread_06 = new Thread(targetRunnable, thread_06Name);
Thread thread_07 = new Thread(mainThreadGroup, targetRunnable, thread_07Name);
Thread thread_08 = new Thread(mainThreadGroup, targetRunnable, thread_08Name, stackSize);
thread_01.start();
thread_02.start();
thread_03.start();
thread_04.start();
thread_05.start();
thread_06.start();
thread_07.start();
thread_08.start();
mainThreadGroup.list();
}
}
1.2 入参说明
- targetRunnable
是接口 Runnable 的实现,需要实现 run() 方法。线程交由 JVM 管理,线程启动后,可运行的线程会执行 run() 的内部逻辑。 - threadGroup
线程组,用来管理线程的,由线程组成;线程对象创建时可指定所属线程组 threadGroup; 未指定线程组则当前子线程线程组默认沿用父线程的线程组 - name
线程名称,默认名称为 Thread-index,index 属于[0, +∞)。线程名称支持自定义,允许重复,不可为空;线程 ID 自增唯一。 - stackSize
指定方法栈大小,避免内存泄漏。超出指定大小则会抛出 java.lang.*Error
关键代码演示:
private static long threadSeqNumber;
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize) {
init(g, target, name, stackSize, null, true);
}
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
// ... ...
Thread parent = currentThread();
SecurityManager security = System.getSecurityManager();
if (g == null) {
if (security != null) {
g = security.getThreadGroup();
}
if (g == null) {
g = parent.getThreadGroup();
}
}
// ... ...
this.group = g;
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
/* Set thread ID */
tid = nextThreadID();
}
2. 两种实现方式
2.1 作为子类继承 Thread 类
class ThreadByExtendsThread extends Thread {
@Override
public void run() {
// super.run() or custom to do something
super.run();
}
}
eg: 线程调用方式
ThreadByExtendsThread thread = new ThreadByExtendsThread();
thread.start();
2.2 作为实现类实现 Runnable 接口
class ThreadByImplementsRunnable implements Runnable {
@Override
public void run() {
// custom to to something
}
}
eg: 线程调用方式
ThreadByImplementsRunnable runnable = new ThreadByImplementsRunnable();
Thread thread = new Thread(runnable);
thread.start();
附:官方文档
在线文档:https://docs.oracle.com/javase/8/docs/api/
离线文档:https://www.oracle.com/java/technologies/javase-jdk8-doc-downloads.html
Power By niaonao
上一篇: Vue中树形结构简单实现
下一篇: 树的直径(两次dfs & 树形dp)