package com.luck;
public class ThreadTestOne {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
MyDown md = new MyDown(10);
System.out.println(Thread.currentThread().getName() + "开始");// 打印开始标记
for (int ii = 0; ii < 10; ii++) {// 开10个线程
Thread t = new ImportThread(md);
t.start();
}
while(true){
if(md.count <= 0){//当10次循环结束的时候 才跳出while循环
break;
}
}
System.out.println(Thread.currentThread().getName() + "结束");// 打印开始标记
}
static class ImportThread extends Thread {
private MyDown c;
public ImportThread(MyDown c) {
this.c = c;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始...");// 打印开始标记
c.test();
// 做一些事情... ...
System.out.println(Thread.currentThread().getName() + "结束.");// 打印结束标记
}
}
//计数器
static class MyDown{
private int count ;
public MyDown(int count){
this.count = count;
}
public synchronized void test(){
count--;
}
}
}
public class ThreadTestOne {
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
MyDown md = new MyDown(10);
System.out.println(Thread.currentThread().getName() + "开始");// 打印开始标记
for (int ii = 0; ii < 10; ii++) {// 开10个线程
Thread t = new ImportThread(md);
t.start();
}
while(true){
if(md.count <= 0){//当10次循环结束的时候 才跳出while循环
break;
}
}
System.out.println(Thread.currentThread().getName() + "结束");// 打印开始标记
}
static class ImportThread extends Thread {
private MyDown c;
public ImportThread(MyDown c) {
this.c = c;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "开始...");// 打印开始标记
c.test();
// 做一些事情... ...
System.out.println(Thread.currentThread().getName() + "结束.");// 打印结束标记
}
}
//计数器
static class MyDown{
private int count ;
public MyDown(int count){
this.count = count;
}
public synchronized void test(){
count--;
}
}
}