java 类锁和对象锁的一段代码,不解释

import java.util.Random;

public class ThreadTests implements Runnable {
	private static  int a = 1;//static 修饰共享数据
	private static int i = 1;

	@Override
	public void run() {
		i += 1;
		operate();
		System.out.println("i:" + i + ",a:" + a + ",TEMP:" + getRandom());
	}

	private static synchronized void operate() {
		a += i;
		//处理共享数据的要在这里进行
	}
	
	private int getRandom(){
		return (new Random()).nextInt(1000);
		//处理各种对象级的个性化操作,线程级别
	}

	public static void main(String[] args) {
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
		new Thread(new ThreadTests()).start();
	}

}

你可能感兴趣的:(java)