簡單講解了關(guān)于多線程的一些應(yīng)用:線程進(jìn)程的概念區(qū)別刨裆,定義多線程,多線程之間的通信彬檀,線程的死鎖帆啃,以及線程的優(yōu)先級。此類問題在Java面試當(dāng)中經(jīng)常觸及窍帝。
1.多線程的實(shí)踐:
package Thread;
public class Live implements Runnable{
public void run() {
for(int i = 1;i<=6;i++){
System.out.println("摳腳"+i+"次");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package Thread;
public class Play extends Thread{
public void run(){
for(int i=1;i<=30;i++){
System.out.println("who打單機(jī):第"+i+"局努潘,輸了,玩的真菜坤学!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package Thread;
/*
* 多線程同時執(zhí)行疯坤,有兩種實(shí)現(xiàn)多線程的方法,一種是繼承類深浮,一種是實(shí)現(xiàn)接口压怠。
* 繼承: 繼承Thread的話,該子類應(yīng)重寫 Thread 類的 run 方法飞苇。接下來可以分配并啟動該子類的實(shí)例
* 實(shí)現(xiàn)接口:聲明實(shí)現(xiàn) Runnable 接口的類菌瘫,該類然后實(shí)現(xiàn) run 方法蜗顽。
* 然后可以分配該類的實(shí)例,在創(chuàng)建 Thread 時作為一個參數(shù)來傳遞并啟動雨让。
* */
public class Thread_jichu {
public static void print(){
for(int i=1;i<=50;i++){
System.out.println("who看泄透牵花的貼身高手:第"+i+"集,看的挺嗨的栖忠!");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread t = new Play();
Live l = new Live();
Thread t1 = new Thread(l);
t1.start();
t.start();
print();
}
}
2.線程安全和同步:
package Thread;
public class Synchronized_tongbu {
public static void main(String[] args) {
BankAccount account = new BankAccount();
Thread husband = new BankThread(account);
Thread wife = new BankThread(account);
husband.start();
wife.start();
}
}
class BankAccount{
private double balance = 1000;
public boolean depasit(double newAdd){
if(newAdd <=0){
return false;
}else{
synchronized(this){//鎖方法和鎖局域代碼
System.out.println("當(dāng)前余額為:"+balance);
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
balance = balance + newAdd;
}
System.out.println("新的余額為:" + balance);
}
return true;
}
}
class BankThread extends Thread{
private BankAccount bankAccount;
public BankThread(BankAccount account){
bankAccount = account;
}
public void run(){
bankAccount.depasit(200);
}
}
死鎖
package Thread;
/*
* 由于加鎖崔挖,所以在取資源時某一方?jīng)]有使用完不得進(jìn)入該空間。
*
* */
public class DeadLock {
public static void main(String[] args) {
Object object1 = new Object();
Object object2 = new Object();
Thread ta = new A(object1, object2);
Thread tb = new B(object1, object2);
ta.start();
tb.start();
}
}
class A extends Thread {
Object object1;
Object object2;
public A(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
public void run() {
synchronized (object1) {
System.out.println("A取到了object1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(object2){
System.out.println("A拿到了object1和object2");
}
}
}
}
class B extends Thread {
public Object object1;
public Object object2;
public B(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
public void run() {
synchronized (object2) {
System.out.println("B取到了object1");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(object1){
System.out.println("B拿到了object1和object2");
}
}
}
}
如上代碼會出現(xiàn)死鎖庵寞,未能A B都未能取到所需虚汛,將B run方法中object2 object1調(diào)換即可。取得順序相同很慶幸解決了問題皇帮。