---
title: "多线程控制执行"
author: "bughope"
date: "2014-06-17T21:06:16Z"
tags: ["java基础", "同步", "多线程", "线程同步"]
categories: ["编程语言"]
slug: "untitled"
draft: true
lastmod: "2014-06-17T21:06:16Z"
id: 2081935
url: "https://bughope.iteye.com/blog/2081935"
---

```
package org.masque.effective;

import java.util.concurrent.TimeUnit;
/**
 * 
 * @author masque.java@gmail.com
 *
 */
public class ThreadTest {
	
    static int count = 0;
	
	public static synchronized int getCount() {
		return count;
	}

	public static synchronized void setCount() {
		count++;
	}

	public static void main(String[] args) throws InterruptedException {
		(new Thread1()).start();
		(new Thread2()).start();
		(new Thread3()).start();
		while (true) {
			TimeUnit.SECONDS.sleep(1);
			if (getCount()==3) {
				System.out.println("over!");
				return;
			}
		}
	}
	
	
	
}

class Thread1 extends Thread{
	@Override
	public void run() {
		for (int i=10;i<200;i++) {
			System.out.println("Thread1===========:"+i);
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		ThreadTest.setCount();
	}
}

class Thread2 extends Thread{
	@Override
	public void run() {
		for (int i=10;i<200;i++) {
			System.out.println("Thread2===========:"+i);
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		ThreadTest.setCount();
	}
}

class Thread3 extends Thread{
	@Override
	public void run() {
		for (int i=10;i<200;i++) {
			System.out.println("Thread3===========:"+i);
			try {
				TimeUnit.SECONDS.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		ThreadTest.setCount();
	}
}
```