继续Thread的类,重新run方法 ```java public class ExtendsThread extends Thread {
@Override public void run() {
System.out.println(Thread.currentThread().getName() + ",我是子线程");
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + ",我是主线程");
new ExtendsThread().run();
ExtendsThread thread = new ExtendsThread();
thread.start();
thread.interrupt();
}
}
2. 实现Runnable接口,重写run方法
```java
public class ImplementRunnable implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + ",我是子线程");
}
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName() + ",我是主线程");
new Thread(() -> System.out.println(Thread.currentThread().getName() + "----")).start();
//
Thread thread = new Thread(new ImplementRunnable());
thread.setName("zs");
thread.start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(Thread.currentThread().getName() + ",我是子线程");
}
}).start();
}
可以带返回结果的的线程 Callable Future
public class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
return "异步执行发送短信";
}
public static void main(String[] args) {
MyCallable myCallable = new MyCallable();
try {
String call = myCallable.call();
System.out.println(call);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FutureTask<String> futureTask = new FutureTask<String>(new MyCallable());
new Thread(futureTask).start();
try {
System.out.println("result: " + futureTask.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
线程池批量创建线程
public class exectorCreateThread {
public static void main(String[] args) {
ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
executor.execute(new Thread(() -> {
System.out.println(Thread.currentThread().getName() + ",我是线程池创建");
}));
}
}
其他扩展方式创建线程: spring提供异步直接@Asyn
多个线程交替打印数字,顺序打印
public class Test {
public static void main(String[] args) {
Data data = new Data();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
data.printA();
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
data.printB();
}
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 10; i++) {
data.printC();
}
}, "C").start();
}
}
class Data{
Lock lock = new ReentrantLock();
// Condition 可以保障精准通知
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
private int num = 1;
private int size = 0;
public void printA() {
lock.lock();
try {
while (num != 1) {
condition1.await();
}
System.out.println(Thread.currentThread().getName() + ":" + size ++);
num = 2;
condition2.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB() {
lock.lock();
try {
while (num != 2) {
condition2.await();
}
System.out.println(Thread.currentThread().getName() + ":" + size ++);
num = 3;
condition3.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC() {
lock.lock();
try {
while (num != 3) {
condition3.await();
}
System.out.println(Thread.currentThread().getName() + ":" + size ++);
num = 1;
condition1.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}