`

java 线程安全例子

    博客分类:
  • java
 
阅读更多
package com.hym.test.thread;

public class CountNum {
	private int count = 0;

	public void count() {
		for (int i = 1; i <= 10; i++) {
			count += i;
		}

		System.out.println("Thread Name is:" + Thread.currentThread().getName()
				+ ", count is:" + count);
	}

	public static void main(String[] args) {
		Runnable runnable = new Runnable() {
			CountNum counter = new CountNum();
			public void run() {
				counter.count();
			}
		};

		for (int i = 0; i < 10; i++) {
			Thread thread = new Thread(runnable);
			thread.start();
		}

	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics