多线程入门

一直只是看看多线程有关的东西, 项目中用的时候也只是网上查查, 能work就好, 而没有真正去研究过。 所以这次打算好好看一下。。。
------------------------ 我是分割线 ------------------------------
一个简单的多线程程序:
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">引用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值