【Java面试】确保线程顺序执行的实现方法

时间:2023-02-22 17:04:27

1.Thread.join()

我们来看看在 Java 7 Concurrency Cookbook 中相关的描述(很清楚地说明了 join() 的作用):

Waiting for the finalization of a thread In some situations, we will have to wait for the finalization of a thread. For example, we may have a program that will begin initializing the resources it needs before proceeding with the rest of the execution. We can run the initialization tasks as threads and wait for its finalization before continuing with the rest of the program. For this purpose, we can use the join() method of the Thread class. When we call this method using a thread object, it suspends the execution of the calling thread until the object called finishes its execution. 当我们调用某个线程的这个方法时,这个方法会挂起调用线程,直到被调用线程结束执行,调用线程才会继续执行。内部使用了wait方法,和sleep的区别是,sleep不会释放锁,而wait会释放锁。

public class Main {
	public static void main(String[] args) throws InterruptedException {
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread3 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});
		thread1.start();
		thread1.join();

		thread2.start();
		thread2.join();

		thread3.start();
		thread3.join();
		System.out.println("Main Thread exit!");
	}
}

主线程这种按照顺序调用了thread1、thread2、thread3,main thread存当了调用者的角色。

2.自定义实现串行执行器

import java.util.ArrayDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Main {
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread3 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		SerialDefineExecutor defineExecutor = new SerialDefineExecutor();
		defineExecutor.execute(thread1);
		defineExecutor.execute(thread2);
		defineExecutor.execute(thread3);

		System.out.println("Main Thread exit!");
	}

	public static class SerialDefineExecutor implements Executor {
		ArrayDeque<Runnable> queue = new ArrayDeque<Runnable>();
		Runnable current = null;
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 5,
				TimeUnit.SECONDS, sPoolWorkQueue);
		private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(
				128);

		public SerialDefineExecutor() {
			threadPoolExecutor.allowCoreThreadTimeOut(true);
		}
		public synchronized void execute(Runnable runnable) {
			queue.offer(new Runnable() {

				@Override
				public void run() {
					try {
						runnable.run();
					} finally {
						next();
					}
				}
			});
			if (current == null) {
				next();
			}
		}

		private synchronized void next() {
			if (!queue.isEmpty()) {
				current = queue.pop();
				threadPoolExecutor.execute(current);
			}
		}
	}
}

看过AsyncTask源码的各位,肯定看到过内部有这个串行执行器,保证内部线程的顺序执行,这里我们可以模仿写一个。

3.Thread.setPriority

设置线程优先级

public class Main {
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread2 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		Thread thread3 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out
						.println(Thread.currentThread().getId() + "running...");
			}
		});

		thread1.setPriority(1);
		thread2.setPriority(2);
		thread3.setPriority(3);
		thread1.start();
		thread2.start();
		thread3.start();
		System.out.println("Main Thread exit!");
	}
}