62,620
社区成员
发帖
与我相关
我的任务
分享package thread;
public class TT implements Runnable {
int b = 100;
public synchronized void m1() throws InterruptedException{
System.out.println(Thread.currentThread().getName()+" m1()的优先级 "+Thread.currentThread().getPriority());
System.out.println("m1()1: b = " + b);
b = 1000;
System.out.println("m1()2: b = " + b);
//Thread.sleep(400);
//System.out.println("m1(): b = " + b);
}
public void m2() throws InterruptedException{
System.out.println(Thread.currentThread().getName()+" m2()的优先级 "+Thread.currentThread().getPriority());
//Thread.sleep(100);
System.out.println("m2()1: b = " + b);
b = 2000;
System.out.println("m2()2: b = " + b);
}
@Override
public void run() {
try {
m1();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
TT tt = new TT();
Thread thread = new Thread(tt);
thread.setPriority(Thread.MAX_PRIORITY);
System.out.println(Thread.currentThread().getName()+Thread.currentThread().getPriority());
thread.start();
tt.m2();
System.out.println(tt.b);
}
}