Thread Overview

It is just reference Services.

Almost this Code is copied from Apache' Avalon-Cornerstone.
Cornerstone is a good Component. but It can only run on Avalon Framework. so I just modified them to remove the coupling with Avalon Framework

Usage Example

services.xml

<?xml version="1.0" encoding="UTF-8"?>

<services>
	<service id="ThreadManager" 
	  class="sunheart.third.thread.impl.ResourceLimitingThreadManager" />
</services>          
          

Java code

// Worker.java
package example;

/**
 *  
 */
public class Worker implements Runnable {

	int waitTime;
	public Worker(int waitTime) {
		this.waitTime = waitTime;
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		System.out.println("start:" + Thread.currentThread());

		try {
			Thread.sleep(waitTime * 1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("end:" + Thread.currentThread());

	}
}

// ThreadTest.java
package example;

import sunheart.service.ServiceManager;
import sunheart.service.thread.ThreadManager;

public class ThreadTest {

	public static void main(String[] args) {

		System.out.println("main start");

		ThreadManager threadManager = (ThreadManager) ServiceManager
				.lookupService(ThreadManager.id);

		ThreadTest test = new ThreadTest();

		Worker a = new Worker(3);
		Worker b = new Worker(2);

		threadManager.getDefaultThreadPool().execute(a);
		threadManager.getDefaultThreadPool().execute(b);

		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("main end");
	}

}