目录
1、线程创建
2、线程中断
3、线程等待
4、线程休眠
5、获取线程实例
1、线程创建的5种方法(非全部)
方法①:创建一类来继承Thraed,重新run方法
class MyThread extends Thread{ //创建一个MyThread类来继承Thread类。
@Override
public void run() { //重写run方法
//线程需要执行的代码
}
}
public class demo4 {
public static void main(String[] args) {
MyThread t = new MyThread(); //创建MyThread类的实例
t.start(); //调用start方法来启动线程
}
}
方法②:实现Runnable 接口来重新Run方法来创建新线程
class MyRunnable implements Runnable{ //实现Runnable接口
@Override
public void run() { //重新run方法
//这里时线程所需要执行的代码
}
}
public class demo5 {
public static void main(String[] args) {
//创建Thread实例,调用Thread的构造方法时将Runnable对象作为参数
MyRunnable myRunnable = new MyRunnable();
Thread t = new Thread(myRunnable);
t.start(); //调用start方法来启动线程
}
}
方法③:使用匿名内部类实现 创建Thread 子类的方式来创建一个新线程
public class demo6 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() { //重新run方法
//这里是线程要执行的代码
}
};
t.start(); //调用start方法来启动新线程
}
}
方法④:使用匿名内部类实现 实现Runnable接口来创建新线程
public class demo7 {
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
//这里时线程要执行的代码
}
});
t.start();//启动新线程
}
}
方法⑤:使用lambda表达式来创建新线程
public class demo8 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
//线程要执行的代码
});
t.start();//启动新线程
}
}
2、线程中断
方法①:使用自定义变量来作为标志位
public class demo9 {
private static boolean isQuit = true;//设立标志位
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() -> {
while(isQuit){
System.out.println("hello Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("线程已中断");
});
t.start();
Thread.sleep(3000);
System.out.println("中断线程");
isQuit = false;//修改标志位使线程中断
}
}
方法②:使用Thread.currentThread().isInterrupted()来代替自定义标志位
public class demo10 {
public static void main(String[] args){
Thread t = new Thread(() -> {
while(!Thread.currentThread().isInterrupted()){//设置标志位其中Thread.currentThread().isInterrupted()会返回false
System.out.println("hello Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
//throw new RuntimeException(e);
break;//抛出异常时进入catch来跳出循环
}
}
System.out.println("线程已中断");
});
t.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("中断线程");
t.interrupt();//调用interrupt来设置标志位,如果线程正在运行状态会设置标志位为true,
//如果线程为阻塞状态(sleep)就会抛出InterruptedException异常来提前唤醒sleep从而执行catch中的代码
}
}
3、线程等待
方法①:调用jion方法
public class demo11 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("hello Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
try {
t.join();//使线程执行结束以后再继续往下执行
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("5次hello Thread已经打印完成");
System.out.println("结束进程");
}
}
方法②:利用jion(long millis),jion(long millis,int nanos)。括号中的是等待时间与方法①中的等到线程结束不同
public class demo11 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("hello Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
try {
t.join(1000,500);//使线程执行结束以后再继续往下执行,前者毫秒后者纳秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("5次hello Thread已经打印完成");
System.out.println("结束进程");
}
}
4、线程休眠
利用sleep来时进程进入休眠状态
public class demo12 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("hello Thread");
try {
Thread.sleep(1000);//使用sleep放法来使线程进入休眠括号中参数为时间单位为毫秒
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
t.start();
}
}
5、获取线程实例
方法:利用currentThread方法来放回当前线程对象的引用
public class demo13 {
public static void main(String[] args) {
Thread t = new Thread(() -> {
while (true) {
System.out.println("hello Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"这是一个新线程");//命名线程的名称
t.start();
t.currentThread();//返回当前线程对象的引用
System.out.println(t.getName());//获取当前线程的名称
}
}