Home Blog Java Notes

Print strings in sequence with multiple threads

Published:


public class MTest {

	public static void main(String[] args) throws InterruptedException {

		Thread t1 = new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 1000; i ++) {
					System.out.println("hello " +  i);
				}
			}
		});

		Thread t2 = new Thread(new Runnable() {
			public void run() {
				System.out.println("firstName");
			}
		});

		Thread t3 = new Thread(new Runnable() {
			public void run() {
				System.out.println("lastName");
			}
		});

		t1.start();
		t1.join();
		t2.start();
		t2.join();
		t3.start();

	}

}
Comment