[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/kylez7/JavaCodeAcc/master/src/concurrency/ThreadLifeCycle.java [Back]  [Original]

package concurrency;

import java.util.concurrent.TimeUnit;

/**
 *  NEW RUNNABLE TIMED_WAITING WAITING BLOCKED TERMINATED
 * 
 * @author liu yuning
 *
 */

class Block {
    public boolean waitStatus = true;

    public void waitOp() throws InterruptedException {
	synchronized (this) {
	    System.out.println(Thread.currentThread().getName() + "  wait");
	    wait();
	    System.out.println(Thread.currentThread().getName()
		    + "  next to Block.wait(), in the loop");
	}
    }

    public void notifyOp() {
	synchronized (this) {
	    this.waitStatus = false;
	    notifyAll();
	    System.out.println(Thread.currentThread().getName()
		    + "  waitnotify");
	}
    }

}

class WaitRunnable implements Runnable {
    private Block block;

    public WaitRunnable(Block block) {
	super();
	this.block = block;
    }

    @Override
    public void run() {
	try {
	    System.out.println(Thread.currentThread().getName()
		    + "  ...");

	    // sleep
	    System.out.println(Thread.currentThread().getName()
		    + "  sleep 2s");
	    TimeUnit.SECONDS.sleep(2);
	    System.out
		    .println(Thread.currentThread().getName() + "  sleep");

	    // sleepwait
	    while (block.waitStatus) {
		block.waitOp();
		System.out.println(Thread.currentThread().getName()
			+ "  next to WaitRunnable.waitOp(), in the loop");
	    }
	    System.out.println(Thread.currentThread().getName()
		    + "  out of wait loop");

	    System.out.println(Thread.currentThread().getName()
		    + "  ...");

	} catch (InterruptedException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }
}

class NotifyRunnable implements Runnable {
    private Block block;

    public NotifyRunnable(Block block) {
	super();
	this.block = block;
    }

    @Override
    public void run() {
	System.out.println(Thread.currentThread().getName() + "  ...");
	block.notifyOp();

	synchronized (block) {
	    try {
		System.out.println(Thread.currentThread().getName()
			+ "  sleep 6s");
		TimeUnit.SECONDS.sleep(6);
	    } catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	    }
	}

	System.out.println(Thread.currentThread().getName() + "  sleep");

    }
}

public class ThreadLifeCycle {
    private static final String IN_THE_STATE = "";
    private static final String ISALIVE_RETRUN = "isAlive()";

    /**
     * Describe the status of a thread
     * 
     * @param threadName
     * @param threadStatus
     * @param isAlive
     * @return thread status description
     */
    public static String descStatus(String threadName, String threadStatus,
	    boolean isAlive) {
	return threadName + "  " + IN_THE_STATE + threadStatus + "  "
		+ ISALIVE_RETRUN + isAlive;
    }

    /**
     * print the status of a thread
     * 
     * @param thread
     */
    public static void printStatus(Thread thread) {
	System.out.println(descStatus(thread.getName(), thread.getState()
		.toString(), thread.isAlive()));
    }

    public static void main(String[] args) throws InterruptedException {
	// 
	Block block = new Block();

	// NEW
	Thread thread = new Thread(new WaitRunnable(block));
	Thread notifyThread = new Thread(new NotifyRunnable(block));

	printStatus(thread);
	printStatus(notifyThread);

	// RUNNABLE
	thread.start();
	printStatus(thread);

	TimeUnit.SECONDS.sleep(1);

	// TIMED_WAITING
	printStatus(thread);

	TimeUnit.SECONDS.sleep(2);

	// WAITING
	printStatus(thread);

	TimeUnit.SECONDS.sleep(1);
	notifyThread.start();
	printStatus(notifyThread);

	TimeUnit.SECONDS.sleep(1);

	// BLOCKED or TERMINATED
	// TERMINATEDnotifyThreadnotifythreadthreadnotifyThread
	printStatus(thread);

	printStatus(notifyThread);

	TimeUnit.SECONDS.sleep(6);

	// TERMINATED
	printStatus(thread);

    }
}

Web Proxy Viewer  |  New URL  |  Original Page