public class TT implements Runnable {
int b = 100;
public synchronized void m1() throws Exception{
b = 1000;
Thread.sleep(5000);//线程睡眠5秒
System.out.println("b = " + b);//打印b的值
}
public void m2() throws Exception {
System.out.println(b);//打印b的值
}
public void run() {//重写线程类的run方法
try {
m1();
} catch(Exception e) {//捕获异常
e.printStackTrace();//打印异常信息
}
}
public static void main(String[] args) throws Exception {//main函数,不该在抛出异常了。否则会有问题
TT tt = new TT();//创建对象tt
Thread t = new Thread(tt);//为tt分配一个新的线程
t.start();//通知cpu线程已经准备就绪
System.out.println("开始执行m2");
//Thread.sleep(1);//睡眠1秒
tt.m2();调用tt的m2方法
}
}
很明显,代码不是你写的,你是要交作业,对个这种懒人问题都不会回答。
没弄明白你是什么意思