线程A,B,C 逐个执行

package com.jibx_maven;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class SingleThreadExecutor {

	public static void main(String[] args) {
		
		ExecutorService executorService = Executors.newSingleThreadExecutor();
//		ExecutorService executorService = Executors.newCachedThreadPool();
		executorService.submit(new Thread(new Worker("A")));
		executorService.submit(new Thread(new Worker("B")));
		executorService.submit(new Thread(new Worker("C")));
	}
}
class Worker implements Runnable {
	
	private String workerId;

	public Worker(String workerId) {
		super();
		this.workerId = workerId;
	}

	@Override
	public void run() {
		Random random = new Random();
		System.out.println("workder " +workerId + " is  starting his work ");
		try {
			Thread.sleep(random.nextInt(10)*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("workder " +workerId+ "finish his work ");
		
	}
	
}


    workder A is  starting his work
    workder Afinish his work
    workder B is  starting his work
    workder Bfinish his work
    workder C is  starting his work
    workder Cfinish his work



你可能感兴趣的:(线程)