?? threadsynchronizdemo3.java
字號(hào):
public class ThreadSynchronizDemo3{
public void 寫(xiě)文件(String 內(nèi)容){
synchronized(this){
System.out.println("進(jìn)入寫(xiě)文件函數(shù)的線程:"+ Thread.currentThread().getName());
System.out.println("獲取當(dāng)前對(duì)象:"+ this + "的鎖");
try{
Thread.sleep(1000*5);
System.out.println("線程:"+ Thread.currentThread().getName() + " 休息了5秒");
}catch(Exception e){}
for(int i=0; i< 內(nèi)容.length(); i++)
System.out.print(內(nèi)容.charAt(i));
System.out.println("\n退出寫(xiě)文件函數(shù)的線程:"+ Thread.currentThread().getName() );
System.out.println("釋放當(dāng)前對(duì)象:"+ this + "的鎖\n");
}
}
public void 創(chuàng)建兩個(gè)線程(){
Runnable r = new Runnable(){
public void run(){
寫(xiě)文件("學(xué)JAVA其實(shí)是件很容易的事,只要你用心學(xué),花時(shí)間");
}
};
Thread 線程A = new Thread(r,"線程A");
Thread 線程B = new Thread(r,"線程B");
線程A.start();
線程B.start();
}
public static void main(String arg[]){
ThreadSynchronizDemo3 demo = new ThreadSynchronizDemo3();
demo.創(chuàng)建兩個(gè)線程();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -