java多线程中方法_java中多线程 - 多线程中的基本方法
介紹一下線程中基本的方法使用
線程睡眠sleep()
Thread.sleep(毫秒);我們可以通過sleep方法設(shè)置讓線程睡眠。可以看到sleep是個(gè)靜態(tài)方法
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守護(hù)線程
非守護(hù)線程停止,那么守護(hù)線程自動(dòng)退出
public static void main(String[] args) {
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 5; i ++) {
System.out.println("非守護(hù)線程");
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
System.out.println("守護(hù)線程");
}
}
};
thread2.setDaemon(true);
thread1.start();
thread2.start();
}
可以很明顯的看到thread2本應(yīng)該執(zhí)行200次輸出,但是這里只輸出了幾行。因?yàn)楫?dāng)thread1執(zhí)行完畢后,thread2作為守護(hù)線程就自動(dòng)停止了。
多線程join
如果執(zhí)行了join方法,那么停止當(dāng)前線程,先跑執(zhí)行了join()的線程。相當(dāng)于插隊(duì)執(zhí)行。如下,在執(zhí)行thread2線程的時(shí)候,如果i==20的時(shí)候,則讓thread1插隊(duì)先執(zhí)行
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println("thread1---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
if (i == 20) {
try {
//插隊(duì)執(zhí)行
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(i);
}
}
};
thread1.start();
thread2.start();
}
join()方法也可以傳參數(shù)long 毫秒 join(毫秒)
表示讓執(zhí)行join的線程,插隊(duì)執(zhí)行XXX毫秒,過了時(shí)間后,兩個(gè)線程交替執(zhí)行
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println("thread1---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 200; i ++) {
if (i == 20) {
try {
//插隊(duì)執(zhí)行1毫秒
thread1.join(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(i);
}
}
};
thread1.start();
thread2.start();
}
yeild 禮讓線程
yeild會(huì)讓出cpu,讓其他線程執(zhí)行
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println( getName() + "---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public 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給線程設(shè)置優(yōu)先級(jí)
默認(rèn)優(yōu)先級(jí)是5 最小1,最大10
越大優(yōu)先級(jí)越高
public static void main(String[] args) {
final Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
for(int i = 0; i < 500; i ++) {
System.out.println( getName() + "---" + i);
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for(int i = 0; i < 500; i ++) {
System.out.println(getName() + "---" + i);
}
}
};
//設(shè)置最大的線程優(yōu)先級(jí)最大為10
thread1.setPriority(Thread.MIN_PRIORITY);
//設(shè)置最小的線程優(yōu)先級(jí),最小為1
thread2.setPriority(Thread.MAX_PRIORITY);
thread1.start();
thread2.start();
}
synchronized
同步代碼塊
當(dāng)多線程并發(fā),多段代碼同時(shí)執(zhí)行的時(shí)候。希望在執(zhí)行其中代碼的時(shí)候,cpu不切換線程
不用synchronized的情況
我們來看一下不用synchronized的情況會(huì)發(fā)生什么
public class ThreadSynchronied {
public static void main(String[] args) {
final Say say = new Say();
Thread thread1 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say();
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say1();
}
}
};
//設(shè)置最大的線程優(yōu)先級(jí)最大為10
thread1.setPriority(Thread.MIN_PRIORITY);
//設(shè)置最小的線程優(yōu)先級(jí),最小為1
thread2.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");
}
}
我們發(fā)現(xiàn)有些輸出并沒有打印全,在執(zhí)行線程thread1的過程中,cpu被thread2搶占。這種情況下,肯定是不符合我們的業(yè)務(wù)邏輯的。所以我們要保證線程執(zhí)行了一個(gè)完整的方法后,cpu才會(huì)被其他線程搶占
使用synchronized
public class ThreadSynchronied {
public static void main(String[] args) {
final Say say = new Say();
Thread thread1 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say();
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say1();
}
}
};
//設(shè)置最大的線程優(yōu)先級(jí)最大為10
thread1.setPriority(Thread.MIN_PRIORITY);
//設(shè)置最小的線程優(yōu)先級(jí),最小為1
thread2.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同步代碼塊后,就發(fā)現(xiàn)不會(huì)出現(xiàn)上述情況了
同步方法
public class ThreadSynchroniedMethod {
public static void main(String[] args) {
final Say say = new Say();
Thread thread1 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say();
}
}
};
Thread thread2 = new Thread() {
@Override
public void run() {
for (int i = 0 ; i < 10000 ; i ++) {
say.say1();
}
}
};
//設(shè)置最大的線程優(yōu)先級(jí)最大為10
thread1.setPriority(Thread.MIN_PRIORITY);
//設(shè)置最小的線程優(yōu)先級(jí),最小為1
thread2.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");
}
}
}
同步方法指的就是在方法上加鎖
靜態(tài)同步方法的所對(duì)象是該類的字節(jié)碼對(duì)象
非靜態(tài)的同步方法鎖對(duì)象是this
多個(gè)線程使用同一資源鎖,容易造成死鎖
什么是死鎖?
死鎖是指兩個(gè)或兩個(gè)以上的進(jìn)程在執(zhí)行過程中,由于競(jìng)爭(zhēng)資源或者由于彼此通信而造成的一種阻塞的現(xiàn)象,若無外力作用,它們都將無法推進(jìn)下去。此時(shí)稱系統(tǒng)處于死鎖狀態(tài)或系統(tǒng)產(chǎn)生了死鎖,這些永遠(yuǎn)在互相等待的進(jìn)程稱為死鎖進(jìn)程。
線程安全類
Vector
StringBuffer
HashTable
線程不安全
ArrayList
StringBuilder
HashSet
java.util.Collections中有synchronizedList等方法,支持我們把線程不安全的集合轉(zhuǎn)成線程安全的
學(xué)習(xí)筆記
多次啟動(dòng)一個(gè)線程是非法的
總結(jié)
以上是生活随笔為你收集整理的java多线程中方法_java中多线程 - 多线程中的基本方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql cluster自动安装_My
- 下一篇: python tuple list_草根