// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)ThreadUtil.java, 1.2, 07/11/96





package marimba.util;





/**


 * This class is a help class for threads. It currently just


 * finds the system thread group so that you can create threads


 * in the appropriate thread group.


 *


 * @author	Arthur van Hoff


 * @version 	1.2, 07/11/96


 */


public class ThreadUtil {


    /**


     * The system thread group.


     */


    public static ThreadGroup systemGroup = getSystemThreadGroup();





    /**


     * Find the system thread group.


     */


    private static ThreadGroup getSystemThreadGroup() {


	ThreadGroup g = Thread.currentThread().getThreadGroup();


	while ((g.getParent() != null) && (g.getParent().getParent() != null)) {


	    g = g.getParent();


	}


	return g;


    }





    /**


     * Fork a system thread. If the fork fails due to a security


     * exception, make it a user thread. 


     */


    public static Thread forkSystem(String name, Runnable target) {


	Thread t;


	try {


	    t = new Thread(systemGroup, target, name);


	    t.start();


	    return t;


	} catch (SecurityException e) {


	    return fork(name, target);


	}


    }





    /**


     * Fork a user thread.


     */


    public static Thread fork(String name, Runnable target) {


	Thread t = new Thread(target, name);


	t.start();


	return t;


    }


}


