1.jion方法
注意:
如:下例中timeThread.start()若没有则TimeThread线程不会执行,jion阻塞不起作用
例子:
package texts;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Home {
public static void main(String[] args) throws InterruptedException {
TimeThread timeThread = new TimeThread();
timeThread.start();
new CounterThread(timeThread).start();//更强势优先执行
}
}
class CounterThread extends Thread{
TimeThread timeThread;
public CounterThread(TimeThread timeThread) {
this.timeThread = timeThread;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(i);
if (i==2) {
try {
timeThread.join();//执行join方法的线程和调用囧方法的线程不是同一个 jion阻塞CounterThread线程
//故而TimeThread线程开始执行
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class TimeThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sd.format(date));
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.interrupt方法
结束线程在调用Object类的wait方法,jion方法,sleep方法过程中的阻塞状态,并在调用wait,jion,sleep方法处产生InterruptedException异常
如上图加上counterThread.interrupt();之后当i==2时本该被阻塞的CounterThread线程未被阻塞继续输出且报异常,输出完后到TimeThread线程执行
3.setDaem方法
• setDaemon方法:用于将一个尚未调用线程start方法的线程设置为守护线程。守护线程主要用于为其他线程的运行提供服务(Java中的垃圾回收机制就是守护线程),这种线程属于创建它的线程,守护线程随着主线程的终止而终止。
例子:
package texts;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Home {
public static void main(String[] args) throws InterruptedException {
TimeThread timeThread = new TimeThread();
timeThread.setDaemon(true);
timeThread.start();
System.out.println("jjjjjjjjj");
}
}
class CounterThread extends Thread{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
}
}
class TimeThread extends Thread{
@Override
public void run() {
while(true) {
Date date = new Date();
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(sd.format(date));
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
则只输出jjjjjjjjj
注意:线程中所启动的其他非守护线程线程不会随着该线程的结束而结束
加上CounterThread counterThread = new CounterThread(timeThread);
counterThread.start();后则CounterThread 线程还会继续执行
4.currentThread方法
返回当前正在执行的线程对象
例子:
代码1:
public class Test {
public static void main(String[] args) {
Thread thread = new TimeThread();
System.out.println(thread);
thread.start();
}
}
class TimeThread extends Thread{
@Override
public void run() {
Thread thread = Thread.currentThread();
System.out.println(thread);
}
}
代码2:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
new TimeThread().start();
}
}
class TimeThread extends Thread{
public TimeThread(){
super("时间线程");
}
@Override
public void run() {
printTime();
}
public void printTime(){
Thread thread = Thread.currentThread();
String time = new SimpleDateFormat("HH:mm:ss").format(new Date());
System.out.println(thread.getName()+",当前时间:"+time);
}
}
代码3:
public class Test {
public static void main(String[] args) {
TimeThread timeThread = new TimeThread();
System.out.println("########"+timeThread);
timeThread.start();
timeThread.run();//方法不是线程,故而正在执行得到线程对象还是主线程
}
}
class TimeThread extends Thread{
@Override
public void run() {
Thread thread = Thread.currentThread();
System.out.println("@@@@@@@@"+thread);
}
}
代码4:
public class Test {
public static void main(String[] args) {
TimeThread timeThread = new TimeThread();
System.out.println("########"+timeThread);
timeThread.start();
timeThread.main();
}
}
class TimeThread extends Thread{
@Override
public void run() {
main();
}
void main(){
Thread thread = Thread.currentThread();
System.out.println("@@@@@@@@"+thread);
}
}
5.isAlive方法
判定该线程是否处于就绪、运行或阻塞状态,如果是则返回true,否则返回false
例子:
package texts;
public class Home {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
new PrintThread(thread).start();
System.out.println("main线程状态:"+thread.isAlive());
}
}
class PrintThread extends Thread{
private Thread thread;
public PrintThread(Thread thread){
this.thread = thread;
}
@Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println("main线程状态:"+thread.isAlive());
}
}
先输出main线程状态:true;此时执行sleep(1000);使 PrintThread 线程阻塞所以输出为true。再输出main线程状态:false;因为 System.out.println(“main线程状态:”+thread.isAlive());此时线程已经结束。