深入理解多线程

来源:互联网 发布:哈尔滨师范大学数据 编辑:程序博客网 时间:2024/06/06 12:42
public class ThreadDemo {


public static void main(String[] args) {
Mythread Mt = new Mythread();
Mythread Mt2 = new Mythread();


Mt.start();
Mt2.start();
}


}

public class Mythread extends Thread {
static int i = 0;
@Override
public synchronized void start() {
System.out.println("线程正在开始");
}


@Override
public void run() {
try {

while (i < 100) {
Thread.sleep(2000);
i++;
System.out.println(Thread.currentThread() + "输出的第" + i + "个数");
System.out.println("线程已经运行");
}
} catch (Exception e) {


}


}


}

原创粉丝点击