ITeye博客

单线程问题

arne3166 · 2010-10-14 10:57 · 多线程 · 编程语言 · Markdown

Java中单线程的写法

import java.io.*;

public class WithoutThread {
	static PrintWriter out = new PrintWriter(System.out, true);
	public static void main(String[] args) {
		NoThreadPseudoIO pseudo = new NoThreadPseudoIO();
		pseudo.run();
		showElapsedTime("Another task starts");
	}
	static long baseTime = System.currentTimeMillis();
	static void showElapsedTime(String message) {
		long elapseTime = System.currentTimeMillis() - baseTime;
		out.println( message + "at" + (elapseTime / 1000.0) + "seconds"); 
	}
}
class NoThreadPseudoIO {
	int data = -1;
	NoThreadPseudoIO() {
		WithoutThread.showElapsedTime("NoThreadPseudoIO created");
	}
	public void run() {
		WithoutThread.showElapsedTime("NoThreadPseudoIO starts");
		try {
			Thread.sleep(10000);
			data = 999;
 			WithoutThread.showElapsedTime("NoThreadPseudoIO finishes");
		}catch (InterruptedException e) {}
	}
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。