介紹一下線程中基本的方法使用
線程睡眠sleep()
Thread.sleep(毫秒);我們可以通過sleep方法設置讓線程睡眠。可以看到sleep是個靜態方法
public static native void sleep(long var0) throws InterruptedException;
try {System.out.println(new Date().getSeconds());Thread.sleep(5000);System.out.println(new Date().getSeconds());} catch (InterruptedException e) {e.printStackTrace();}
setDaemon守護線程
非守護線程停止,那么守護線程自動退出
public static void main(String[] args) {Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 5; i ++) {System.out.println("非守護線程");}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {System.out.println("守護線程");}}};thread2.setDaemon(true);thread1.start();thread2.start();}
可以很明顯的看到thread2本應該執行200次輸出,但是這里只輸出了幾行。因為當thread1執行完畢后,thread2作為守護線程就自動停止了。
多線程join
如果執行了join方法,那么停止當前線程,先跑執行了join()的線程。相當于插隊執行。如下,在執行thread2線程的時候,如果i==20的時候,則讓thread1插隊先執行
public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println("thread1---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i == 20) {try {//插隊執行thread1.join();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();}
join()方法也可以傳參數long 毫秒 join(毫秒)
表示讓執行join的線程,插隊執行XXX毫秒,過了時間后,兩個線程交替執行
public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println("thread1---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i == 20) {try {//插隊執行1毫秒thread1.join(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(i);}}};thread1.start();thread2.start();}
yeild 禮讓線程
yeild會讓出cpu,讓其他線程執行
public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println( getName() + "---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 200; i ++) {if (i % 5 == 0) {Thread.yield();}System.out.println(getName() + "---" + i);}}};thread1.start();thread2.start();}
setPriority給線程設置優先級
默認優先級是5 最小1,最大10
越大優先級越高
public static void main(String[] args) {final Thread thread1 = new Thread() {@Overridepublic void run() {super.run();for(int i = 0; i < 500; i ++) {System.out.println( getName() + "---" + i);}}};Thread thread2 = new Thread() {@Overridepublic void run() {for(int i = 0; i < 500; i ++) {System.out.println(getName() + "---" + i);}}};//設置最大的線程優先級最大為10thread1.setPriority(Thread.MIN_PRIORITY);//設置最小的線程優先級,最小為1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
synchronized
同步代碼塊
當多線程并發,多段代碼同時執行的時候。希望在執行其中代碼的時候,cpu不切換線程
不用synchronized的情況
我們來看一下不用synchronized的情況會發生什么
public class ThreadSynchronied {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//設置最大的線程優先級最大為10thread1.setPriority(Thread.MIN_PRIORITY);//設置最小的線程優先級,最小為1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {void say() {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}void say1() {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}
}
我們發現有些輸出并沒有打印全,在執行線程thread1的過程中,cpu被thread2搶占。這種情況下,肯定是不符合我們的業務邏輯的。所以我們要保證線程執行了一個完整的方法后,cpu才會被其他線程搶占
使用synchronized
public class ThreadSynchronied {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//設置最大的線程優先級最大為10thread1.setPriority(Thread.MIN_PRIORITY);//設置最小的線程優先級,最小為1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {String s = "hahaah";void say() {synchronized (s) {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}}void say1() {synchronized (s) {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}}
}
使用synchronized同步代碼塊后,就發現不會出現上述情況了
同步方法
public class ThreadSynchroniedMethod {public static void main(String[] args) {final Say say = new Say();Thread thread1 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say();}}};Thread thread2 = new Thread() {@Overridepublic void run() {for (int i = 0 ; i < 10000 ; i ++) {say.say1();}}};//設置最大的線程優先級最大為10thread1.setPriority(Thread.MIN_PRIORITY);//設置最小的線程優先級,最小為1thread2.setPriority(Thread.MAX_PRIORITY);thread1.start();thread2.start();}
}class Say {//在方法上加鎖static synchronized void say() {System.out.print("s ");System.out.print("a ");System.out.print("y ");System.out.print("h ");System.out.print("e ");System.out.print("l ");System.out.print("l ");System.out.println("o");}static void say1() {synchronized (Say.class) {System.out.print("1 ");System.out.print("2 ");System.out.print("3 ");System.out.print("4 ");System.out.print("5 ");System.out.print("6 ");System.out.print("7 ");System.out.println("8");}}
}
同步方法指的就是在方法上加鎖
靜態同步方法的所對象是該類的字節碼對象
非靜態的同步方法鎖對象是this
多個線程使用同一資源鎖,容易造成死鎖
什么是死鎖?
死鎖是指兩個或兩個以上的進程在執行過程中,由于競爭資源或者由于彼此通信而造成的一種阻塞的現象,若無外力作用,它們都將無法推進下去。此時稱系統處于死鎖狀態或系統產生了死鎖,這些永遠在互相等待的進程稱為死鎖進程。
線程安全類
Vector
StringBuffer
HashTable
線程不安全
ArrayList
StringBuilder
HashSet
java.util.Collections中有synchronizedList等方法,支持我們把線程不安全的集合轉成線程安全的
學習筆記
多次啟動一個線程是非法的