⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread_2.java

📁 java 线程 静态锁
💻 JAVA
字号:
package thread3;

class TestThread {
	     public   void execute(){   //synchronized ,,,,和 下面的 execute1() 是等效的!!
	    	 synchronized (this){   //synchronized ,,,,和 下面的 execute1() 是等效的!!
			    	 for(int i=0;i<20;i++){
		        	  try {
						Thread.sleep(100);
					  } catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					  }
		               System.out.println(Thread.currentThread().getName()+Thread.currentThread().getName()+"    "+i);
			         } 
	    	 }
    }
	     
	     public  synchronized void execute1(){  //synchronized,,,,和上面的 execute() 是等效的!!
			    	 for(int i=0;i<20;i++){
		        	  try {
						Thread.sleep(100);
					  } catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					  }
		               System.out.println(Thread.currentThread().getName()+Thread.currentThread().getName()+"    "+i);
			         } 
    }
}

 class ThreadA implements Runnable{
         TestThread test=null;
	    public ThreadA(TestThread pTest){  //对象有外部引入,这样保证是同一个对象
             test=pTest;
        }
        public void run() {
            test.execute();
}
    }
public class Thread_2{
			public static void main(String[] args){
				 TestThread test=new TestThread();
				 Runnable runabble=new ThreadA(test);
				 Thread a=new Thread(runabble,"A");                
				 a.start();
				 Thread b=new Thread(runabble,"B");
				 b.start();
      }
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -