- public class TestDemo implements Runnable {
- public static void main(String[] args) {
- TestDemo t = new TestDemo();
- new Thread(t).start();
- try{
- Thread.sleep(2000);
- }catch(Exception e) {
- }
- t.study();
-
- }
-
- @Override
- public void run() {
- synchronized(this) { //先不改程序运行,观看结果,然后再改成这样运行,比较一下结果: synchronized(TestDemo.class) ,你就该知道静态是哪个锁了
- while(true){
- System.out.println("run...");
- }
- }
- }
-
- public synchronized static void study() {
- while(true){
- try{
- System.out.println("study...");
- //Thread.sleep(2000);
- }catch(Exception e) {
- }
- }
- }
- }
复制代码 |