---
title: "List非线程安全的代码示例"
author: "joeblow"
date: "2010-12-23T21:50:39Z"
tags: []
categories: ["编程语言"]
slug: "list"
draft: false
lastmod: "2010-12-23T21:50:39Z"
id: 850655
url: "https://joeblow.iteye.com/blog/850655"
---

```java

public class TreadSafeTest {
	//	Vector<Integer> data = new Vector<Integer>();
	//
	List<Integer> data = new ArrayList<Integer>(1);
	{
		data.add(99);
	}

	public static void main(String[] args) throws Exception {
		new TreadSafeTest().test();
	}

	public void test() throws Exception {
		new Thread() {
			int count = 10000;

			public void run() {
				try {
					while (count < 10009) {
						count++;
						System.out.println("1:" + data.get(0));
						data.set(0, count);
						System.out.println("1:" + data.get(0));
					}
				} catch (Exception e) {
					System.out.println(e);
				}
			};
		}.start();
		new Thread() {
			int count = 20000;

			public void run() {
				try {
					while (count < 20009) {
						count++;
						System.out.println("2:" + data.get(0));
						data.set(0, count);
						System.out.println("2:" + data.get(0));
					}
				} catch (Exception e) {
				}
			};
		}.start();
	}

}

```