创建线程
方式1 - 继承Thead类,重写run()方法
public class MyThread extends Thread{
@Override
public void run() {
System.out.println("线程操作");
}
}
public static void main(String[] args) throws Exception {
new MyThread().start();
}
方式2 - 实现Runnable类,重写run()方法
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("线程操作");
}
}
public static void main(String[] args) throws Exception {
new Thread(new MyRunnable()).start();
}
方式3 - 使用lombda
public static void main(String[] args) throws Exception {
new Thread(() -> {
System.out.println("线程操作");
}).start();
}
方式4 - 实现Callable类,重写call()方法
public class MyCallable implements Callable{
@Override
public String call() throws Exception {
System.out.println("线程操作");
return "call";
}
}
public static void main(String[] args) throws Exception {
FutureTask futureTask = new FutureTask<>(new MyCallable());
new Thread(futureTask).start();
// 获取call()的返回值,此方法会阻塞
futureTask.get();
}
方式5 - 使用线程池
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("线程操作");
}
}
public class MyCallable implements Callable{
@Override
public String call() throws Exception {
System.out.println("线程操作");
return "call";
}
}
public static void main(String[] args) throws Exception {
ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(new MyRunnable());
Future future = executorService.submit(new FutureTask<>(new MyCallable()));
future.get();
}
停止线程
方式1 - 使用stop()方法
public class MyRunnable implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("线程操作");
}
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new MyRunnable());
thread.start();
Thread.sleep(500);
// 停止线程
thread.stop();
}
方式2 - 使用suspend()和resume()方法
public class MyRunnable implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("线程操作");
}
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new MyRunnable());
thread.start();
Thread.sleep(500);
// 暂停线程
thread.suspend();
// 恢复线程执行
thread.resume();
}
方式3 - 配合volatile
private static volatile boolean isStop = false;
public static class MyRunnable implements Runnable {
@Override
public void run() {
while (!isStop) {
System.out.println("线程操作");
}
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new MyRunnable());
thread.start();
Thread.sleep(500);
// 修改变量
isStop = true;
}
方式4 - 使用interrupt()方法
public class MyRunnable implements Runnable {
@Override
public void run() {
while (!Thread.interrupted()) {
System.out.println("线程操作");
}
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new MyRunnable());
thread.start();
Thread.sleep(500);
// 打断线程
thread.interrupt();
}
方式5 - 什么都不做,让线程自然结束
public static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("线程操作");
}
}
public static void main(String[] args) throws Exception {
Thread thread = new Thread(new MyRunnable());
thread.start();
}