1.线程创建
(1)继承Thread
public class ThreadDemo1 {
public static void main(String[] args) {
Thread thread=new MyThread();
thread.start();
}
}
class MyThread extends Thread{
public void run(){
System.out.println("hello");
}
}
(2)实现Runnable
(2.1)实现 Runnable 接⼝
public class ThreadDemo2 {
static class MyThread implements Runnable{
@Override
public void run() {
System.out.println("hello");
}
}
public static void main(String[] args) {
MyThread myThread=new MyThread();
Thread thread=new Thread(myThread);
thread.start();
}
}
(2.2)匿名 Runnable
public class ThreadDemo4 {
public static void main(String[] args) {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
});
thread.start();
}
}
(3)使⽤ Lambda
public class ThreadDemo5 {
public static void main(String[] args) {
Thread thread=new Thread(()->{
System.out.println("hello");
});
thread.start();
}
}
(4)带返回值的 Callable
public class ThreadDemo6 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask<Integer> futureTask=new FutureTask<>(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return 0;
}
});
Thread thread=new Thread(futureTask);
thread.start();
int ret=futureTask.get();
}
}
2.线程中断
(1)⾃定义标记符
public class ThreadDemo7 {
private static volatile boolean flag = false;
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
while(!flag){
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
},"子线程");
thread.start();
flag=true;
}
}
(2)使⽤Interrupt()
public class ThreadDemo8 {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
while(!Thread.interrupted()){
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(1));
} catch (InterruptedException e) {
break;
}
}
}
},"子线程");
thread.start();
Thread.sleep(3000);
thread.interrupt();
}
}
3.线程等待 join
public void join() 等待线程结束 ;
public void join(long millis) 等待线程结束,最多等 millis 毫秒 ;
public void join(long millis, int nanos) 同理,但可以更⾼精度
public class ThreadDemo9 {
public static void main(String[] args) throws InterruptedException {
Thread thread=new Thread(()->{
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread.start();
thread.join();
Thread thread1=new Thread(()->{
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
thread1.start();
}
}
4.线程休眠
(1)使⽤ sleep 休眠
public static void sleep(long millis) throws InterruptedException 休眠当前线程 millis 毫秒
public static void sleep(long millis, int nanos) throws InterruptedException 可以更⾼精度的休眠
、
public class ThreadDemo10 {
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(4*1000);
System.out.println(System.currentTimeMillis());
}
}
(2)使⽤ TimeUnit 休眠
TimeUnit.DAYS.sleep(1);//天
TimeUnit.HOURS.sleep(1);//⼩时
TimeUnit.MINUTES.sleep(1);//分
TimeUnit.SECONDS.sleep(1);//秒
5.获取线程实例
public class ThreadDemo {
public static void main(String[] args) {
Thread thread = new Thread();
}
}
本文介绍了Java中创建线程的多种方式,包括继承Thread类、实现Runnable接口、使用Lambda表达式及Callable接口等,并详细讲解了线程的中断、等待和休眠等基本操作。
980

被折叠的 条评论
为什么被折叠?



