java线程安全例子

public class Test {
	public static void main(String[] args) {
		Runnable runnable = new Runnable() {
			public void run() {
				synchronized(this){
				for (int i = 0; i < 10; i++) {
					System.out.print(Thread.currentThread().getName()+"**"+i+"**");
				}
				System.out.println();
			}
			}
		};
		
		Runnable runnable1 = new Runnable() {
			@Override
			public void run() {
				System.out.println("run ...");
			}
		};
		
		for (int i = 0; i < 10; i++) {
			new Thread(runnable).start();
		}
	}
}


输出如下

Thread-0**0**Thread-0**1**Thread-0**2**Thread-0**3**Thread-0**4**Thread-0**5**Thread-0**6**Thread-0**7**Thread-0**8**Thread-0**9**
Thread-3**0**Thread-3**1**Thread-3**2**Thread-3**3**Thread-3**4**Thread-3**5**Thread-3**6**Thread-3**7**Thread-3**8**Thread-3**9**
Thread-2**0**Thread-2**1**Thread-2**2**Thread-2**3**Thread-2**4**Thread-2**5**Thread-2**6**Thread-2**7**Thread-2**8**Thread-2**9**
Thread-1**0**Thread-1**1**Thread-1**2**Thread-1**3**Thread-1**4**Thread-1**5**Thread-1**6**Thread-1**7**Thread-1**8**Thread-1**9**
Thread-9**0**Thread-9**1**Thread-9**2**Thread-9**3**Thread-9**4**Thread-9**5**Thread-9**6**Thread-9**7**Thread-9**8**Thread-9**9**
Thread-8**0**Thread-8**1**Thread-8**2**Thread-8**3**Thread-8**4**Thread-8**5**Thread-8**6**Thread-8**7**Thread-8**8**Thread-8**9**
Thread-7**0**Thread-7**1**Thread-7**2**Thread-7**3**Thread-7**4**Thread-7**5**Thread-7**6**Thread-7**7**Thread-7**8**Thread-7**9**
Thread-6**0**Thread-6**1**Thread-6**2**Thread-6**3**Thread-6**4**Thread-6**5**Thread-6**6**Thread-6**7**Thread-6**8**Thread-6**9**
Thread-4**0**Thread-4**1**Thread-4**2**Thread-4**3**Thread-4**4**Thread-4**5**Thread-4**6**Thread-4**7**Thread-4**8**Thread-4**9**
Thread-5**0**Thread-5**1**Thread-5**2**Thread-5**3**Thread-5**4**Thread-5**5**Thread-5**6**Thread-5**7**Thread-5**8**Thread-5**9**

非线程安全

public class Test {
	public static void main(String[] args) {
		Runnable runnable = new Runnable() {
			public void run() {
				for (int i = 0; i < 10; i++) {
					System.out.print(Thread.currentThread().getName()+"**"+i+"**");
				}
				System.out.println();
			}
		};
		
		Runnable runnable1 = new Runnable() {
			@Override
			public void run() {
				System.out.println("run ...");
			}
		};
		
		for (int i = 0; i < 10; i++) {
			new Thread(runnable).start();
		}
	}
}


输出如下:

Thread-0**0**Thread-2**0**Thread-1**0**Thread-3**0**Thread-2**1**Thread-2**2**Thread-2**3**Thread-2**4**Thread-2**5**Thread-2**6**Thread-0**1**Thread-2**7**Thread-4**0**Thread-3**1**Thread-1**1**Thread-1**2**Thread-1**3**Thread-6**0**Thread-3**2**Thread-5**0**Thread-4**1**Thread-2**8**Thread-0**2**Thread-9**0**Thread-2**9**
Thread-4**2**Thread-5**1**Thread-3**3**Thread-3**4**Thread-3**5**Thread-3**6**Thread-3**7**Thread-3**8**Thread-8**0**Thread-8**1**Thread-6**1**Thread-6**2**Thread-6**3**Thread-6**4**Thread-1**4**Thread-6**5**Thread-8**2**Thread-8**3**Thread-8**4**Thread-8**5**Thread-8**6**Thread-3**9**
Thread-5**2**Thread-4**3**Thread-4**4**Thread-9**1**Thread-9**2**Thread-9**3**Thread-9**4**Thread-9**5**Thread-7**0**Thread-0**3**Thread-7**1**Thread-9**6**Thread-9**7**Thread-4**5**Thread-5**3**Thread-8**7**Thread-6**6**Thread-1**5**Thread-6**7**Thread-8**8**Thread-8**9**
Thread-5**4**Thread-4**6**Thread-4**7**Thread-7**2**Thread-9**8**Thread-9**9**
Thread-0**4**Thread-0**5**Thread-0**6**Thread-7**3**Thread-7**4**Thread-4**8**Thread-4**9**
Thread-5**5**Thread-6**8**Thread-6**9**
Thread-1**6**Thread-5**6**Thread-5**7**Thread-7**5**Thread-7**6**Thread-7**7**Thread-7**8**Thread-7**9**
Thread-0**7**Thread-5**8**Thread-5**9**
Thread-1**7**Thread-0**8**Thread-1**8**Thread-1**9**
Thread-0**9**

你可能感兴趣的:(java线程)