---
title: "多线程"
author: "CaptainCook"
date: "2009-04-17T17:46:02Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-04-24T23:56:20Z"
id: 369788
url: "https://captaincook.iteye.com/blog/369788"
---

```
public class TestThread {

	private int j;

	public static void main(String args[]) {
		TestThread tt = new TestThread();
		Inc inc = tt.new Inc();
		Dec dec = tt.new Dec();
		for (int i = 0; i < 2; i++) {
			Thread t = new Thread(inc);
			t.start();
			t = new Thread(dec);
			t.start();
		}
	}

	private synchronized void inc() {
		j++;
		System.out.println(Thread.currentThread().getName() + "-inc:" + j);
	}

	private synchronized void dec() {
		j--;
		System.out.println(Thread.currentThread().getName() + "-dec:" + j);
	}

	class Inc implements Runnable {
		public void run() {
			for (int i = 0; i < 100; i++) {
				inc();
			}
		}
	}

	class Dec implements Runnable {
		public void run() {
			for (int i = 0; i < 100; i++) {
				dec();
			}
		}
	}

}
```