JavaSE教程_进阶
线程的常用操作
static int |
activeCount() 返回活动线程数量 |
static Thread |
currentThread() 返回当前线程 |
ClassLoader |
getContextClassLoader() 返回类加载器 |
long |
getId() 返回线程的id,每个线程都有唯一 的id |
String |
getName() 返回线程名称. |
int |
getPriority() 返回线程优先级 |
Thread.State |
getState() 返回线程状态 |
void |
interrupt() 中断线程. |
static boolean |
interrupted() 判断线程的中断状态 |
boolean |
isAlive() 判断线程是否终止 |
boolean |
isDaemon() 是否为守护线程 |
boolean |
isInterrupted() 判断线程的中断状态 |
void |
join() 线程合并 |
void |
run() |
void |
setDaemon(boolean on) 设置线程为守护线程 |
void |
setName(String name) 设置线程名称 |
void |
setPriority(int newPriority) 设置优先级 |
static void |
sleep(long millis)线程睡眠(休眠) |
void |
start() 开启新的线程 |
void |
stop() 线程终止 |
String |
toString() |
static void |
yield() 线程让步 |
package com.wkcto.chapter07.method;
/**
* 演示线程的基本操作
* Thread.currentThread() 返回当前线程
* t1.getName() 返回线程名称, 线程默认名称 : Thread-0 / Thread-1
* t1.setName("t1") 设置线程名称
* t1.isAlive() 判断线程是否终止
* Thread.activeCount() 活动线程的数量
* @author 蛙课网
*
*/
public class Test01 {
public static void main(String[] args) {
Thread t1 = new Thread( new Runnable() {
@Override
public void run() {
for( int i = 1; i<=100; i++){
// System.out.println("sub thread: " + i);
System.out.println( Thread.currentThread().getName() + "==>" + i);
}
}
});
t1.setName("t1");
t1.start();
new Thread( new Runnable() {
@Override
public void run() {
for( int i = 1; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "====>" + i);
}
}
}, "t2").start();
System.out.println("11 : " + Thread.activeCount() ); //活动线程的数量
//main线程
for( int i = 1; i<=100; i++){
System.out.println( Thread.currentThread().getName() + "------->" + i);
}
System.out.println( "t1 alive: " + t1.isAlive());
System.out.println("22 : " + Thread.activeCount() ); //活动线程的数量
}
}
package com.wkcto.chapter07.method;
/**
* 守护线程
* 为其他线程服务的, 不能单独运行 , 当JVM中只有守护线程时, JVM退出
* @author 蛙课网
*
*/
public class Test02 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
}
}
} , "t1");
t1.start();
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 500; i++) {
System.out.println(Thread.currentThread().getName() + "----> " + i);
}
}
} , "t2");
t2.setDaemon( true ); //设置线程为守护线程
t2.start();
//main线程
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "==> " + i);
}
}
}
package com.wkcto.chapter07.method;
/**
* 判断线程的状态
* @author 蛙课网
*
*/
public class Test03 {
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
}
}
});
System.out.println("111: " + t1.getState()); //111: NEW 新建状态,刚刚创建了线程对象
t1.start();
System.out.println("222: " + t1.getState()); //222: RUNNABLE 可运行状态,就绪状态, 调用了start()
//main线程
for (int i = 1; i <= 300; i++) {
System.out.println(Thread.currentThread().getName() + "--> " + i);
}
System.out.println("333: " + t1.getState()); //333: TERMINATED 终止状态,线程已结束
}
}
线程的生命周期