一直只是看看多线程有关的东西, 项目中用的时候也只是网上查查, 能work就好, 而没有真正去研究过。 所以这次打算好好看一下。。。
------------------------ 我是分割线 ------------------------------
一个简单的多线程程序:
其某一次运行结果是:
<div class="quote_title">引用
------------------------ 我是分割线 ------------------------------
一个简单的多线程程序:
public class mythread { private list<thread> threads = new arraylist<thread>(); public thread makethread() { runnable runnable = new runnable() { @override public void run() { int i = 0; while (true) { system.out.println(thread.currentthread().getname() + ": " + i++); try { thread.sleep(100); } catch (interruptedexception e) { return; } } } }; return new thread(runnable); } public synchronized void start() { for (int i = 0; i < 10; i++) { mythread th = new mythread(); threads.add(th.makethread()); } for (thread t : threads) t.start(); } public synchronized void stop() { for (thread t : threads) t.interrupt(); threads.clear(); } public static void main(string[] args) throws interruptedexception { system.out.println("all threads will start and end in 1 second..."); system.out.println(new date()); mythread mt = new mythread(); mt.start(); thread.sleep(1000 * 1); mt.stop(); system.out.println(new date()); }}
其某一次运行结果是:
<div class="quote_title">引用