栈中有四个线程:主线程、Thread-0和Thread-1、Thread-2线程。如果有多个线程在排队,那么CPU的执行权是随机抢占的,但是无论哪个线程抢占到CPU的执行权,在线程中都是自上而下执行代码。
代码:
package cx.thread;
public class CreateTherad {
public static void main(String[] args) {
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
});
new thread1().start();
new Thread(new thread2()).start();
thread3.start();
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}}
class thread1 extends Thread {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
}class thread2 implements Runnable {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+ "-"+i);
}
}
}
执行结果:
Thread-1-0
Thread-1-1
Thread-1-2
Thread-1-3
Thread-1-4
Thread-1-5
Thread-1-6
Thread-1-7
Thread-1-8
Thread-1-9
main-0
main-1
main-2
main-3
main-4
main-5
main-6
main-7
main-8
main-9
main-10
main-11
Thread-2-0
Thread-1-10
main-12
Thread-1-11
Thread-1-12
Thread-1-13
Thread-1-14
那么问题来了,如何控制线程执行的顺序呢?
方法1:使用Thread类提供的join方法,可以达到让线程有序执行的效果。
package cx.thread;
public class TheradJoin {
public static void main(String[] args) throws InterruptedException {
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
});
thread11 thread11 = new thread11();
thread11.start();
thread11.join();
Thread thread22 = new Thread(new thread22());
thread22.start();
thread22.join();
thread3.start();
thread3.join();
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}}
class thread11 extends Thread {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
}class thread22 implements Runnable {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+ "-"+i);
}
}
}
方法2:使用线程池
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
代码:
package cx.thread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;public class ThreadPool {
static ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
public static void main(String[] args) {
Thread thread3 = new Thread(new Runnable() { @Override
public void run() {
for (int i = 200; i < 300; i++) {
System.out.println(Thread.currentThread().getName() + "-" + i);
}
}
});
thread111 thread111 = new thread111();
Thread thread222 = new Thread(new thread222());
newSingleThreadExecutor.execute(thread3);
newSingleThreadExecutor.execute(thread111);
newSingleThreadExecutor.execute(thread222);
}
}class thread111 extends Thread {
@Override
public void run() {
for (int i=0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+"-"+i);
}
}
}class thread222 implements Runnable {
@Override
public void run() {
for (int i=100;i<200;i++) {
System.out.println(Thread.currentThread().getName()+ "-"+i);
}
}
}