两个线程调用同一个对象的同一个方法,会怎么样?
public static void main(String[] args) {
// System.out.println(String.valueOf(null));
Test test = new Test();
new Thread(new Runnable() {
@Override
public void run() {
try {
test.ss("线程一");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
test.ss("线程二");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
System.out.println("主程序执行结束");
}
class Test{
public void ss(String s) throws InterruptedException {
for (int i = 0; i < 5; i++) {
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName()+"--->"+s);
}
}
}
测试结果:在五秒内打印结束。