高價(jià)值Java多線程面試題分析
問(wèn)題一
A線程正在執(zhí)行一個(gè)對(duì)象中的同步方法,B線程是否可以同時(shí)執(zhí)行同一個(gè)對(duì)象中的非同步方法?
可以,兩個(gè)線程運(yùn)行所需資源不同,不需要搶占。
案例一、
package duoxiancheng2;
/**
* @author yeqv
* @program A2
* @Classname Ms1
* @Date 2022/2/7 19:08
* @Email w16638771062@163.com
*/
public class Ms1 {
//A線程正在執(zhí)行一個(gè)對(duì)象中的同步方法,B線程是否可以同時(shí)執(zhí)行同一個(gè)對(duì)象中的非同步方法?
Object a = new Object();
public static void main(String[] args) {
var t = new Ms1();
new Thread(() -> t.a1()).start();//A線程
new Thread(() -> t.a2()).start();//B線程
}
void a1() {
synchronized (a) {
System.out.println("同步方法");
}
}
void a2() {
System.out.println("非同步方法");
}
}運(yùn)行結(jié)果:

問(wèn)題二
同上,B線程是否可以同時(shí)執(zhí)行同一個(gè)對(duì)象中的另一個(gè)同步方法?
不可以,兩個(gè)線程執(zhí)行需要一個(gè)共同資源,共同資源加了同步鎖,同一時(shí)刻只能一個(gè)線程占用。
案例二、
package duoxiancheng2;
import java.util.concurrent.TimeUnit;
/**
* @author yeqv
* @program A2
* @Classname Ms2
* @Date 2022/2/7 19:25
* @Email w16638771062@163.com
*/
public class Ms2 {
//同上,B線程是否可以同時(shí)執(zhí)行同一個(gè)對(duì)象中的另一個(gè)同步方法?
Object a = new Object();
public static void main(String[] args) {
var t = new Ms2();
new Thread(() -> t.a1()).start();//A線程
new Thread(() -> t.a2()).start();//B線程
}
void a1() {
synchronized (a) {
System.out.println("進(jìn)入同步方法1");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步方法1結(jié)束");
}
}
void a2() {
synchronized (a) {
System.out.println("進(jìn)入同步方法2");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步方法2結(jié)束");
}
}
}運(yùn)行結(jié)果:
線程A先運(yùn)行,占用資源。

等線程A運(yùn)行完釋放資源后,線程B才可以進(jìn)入執(zhí)行

線程B執(zhí)行完

問(wèn)題三
線程拋出異常會(huì)釋放鎖嗎?
會(huì),線程出現(xiàn)異常拋出后立刻釋放資源。
案例三、
package duoxiancheng2;
import java.util.concurrent.TimeUnit;
/**
* @author yeqv
* @program A2
* @Classname Ms3
* @Date 2022/2/7 19:41
* @Email w16638771062@163.com
*/
public class Ms3 {
//線程拋出異常會(huì)釋放鎖嗎?
Object a = new Object();
public static void main(String[] args) {
var t = new Ms3();
new Thread(() -> t.a1()).start();//A線程
new Thread(() -> t.a2()).start();//B線程
}
void a1() {
int c = 3;
int b;
synchronized (a) {
System.out.println("進(jìn)入同步方法1");
try {
b = c / 0;
System.out.println(b);
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步方法1結(jié)束");
}
}
void a2() {
synchronized (a) {
System.out.println("進(jìn)入同步方法2");
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("同步方法2結(jié)束");
}
}
}結(jié)果: 方法一出現(xiàn)異常,立刻釋放資源。線程二開(kāi)始執(zhí)行

問(wèn)題四
寫(xiě)一個(gè)程序,證明AtomicInteger類比synchronized更高效
synchronized更高效
案例一
package duoxiancheng2;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author yeqv
* @program A2
* @Classname Ms4
* @Date 2022/2/7 20:04
* @Email w16638771062@163.com
*/
public class Ms4 {
AtomicInteger n = new AtomicInteger(10000);
int num = 10000;
public static void main(String[] args) {
var t = new Ms4();
new Thread(t::minus, "T1").start();
new Thread(t::minus, "T2").start();
new Thread(t::minus, "T3").start();
new Thread(t::minus, "T4").start();
new Thread(t::minus, "T5").start();
new Thread(t::minus, "T6").start();
new Thread(t::minus, "T7").start();
new Thread(t::minus, "T8").start();
}
void minus() {
var a = System.currentTimeMillis();
while (true) {
/* if (n.get() > 0) {
n.decrementAndGet();
System.out.printf("%s 售出一張票,剩余%d張票。 %n", Thread.currentThread().getName(), n.get());
} else {
break;
}*/
synchronized (this) {
if (num > 0) {
num--;
System.out.printf("%s 售出一張票,剩余%d張票。 %n", Thread.currentThread().getName(), num);
} else {
break;
}
}
}
var b = System.currentTimeMillis();
System.out.println(b - a);
}
}synchronized結(jié)果:

AtomicInteger結(jié)果:

問(wèn)題五
寫(xiě)一個(gè)程序證明AtomXXX類的多個(gè)方法并不構(gòu)成原子性
package demo16;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 寫(xiě)一個(gè)程序證明AtomXXX類的多個(gè)方法并不構(gòu)成原子性
*/
public class T {
AtomicInteger count = new AtomicInteger(0);
void m() {
for (int i = 0; i < 10000; i++) {
if (count.get() < 100 && count.get() >= 0) { //如果未加鎖,之間還會(huì)有其他線程插進(jìn)來(lái)
count.incrementAndGet();
}
}
}
public static void main(String[] args) {
T t = new T();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < 10; i++) {
threads.add(new Thread(t::m, "thread" + i));
}
threads.forEach(Thread::start);
threads.forEach((o) -> {
try {
//join()方法阻塞調(diào)用此方法的線程,直到線程t完成,此線程再繼續(xù)。通常用于在main()主線程內(nèi),等待其它線程完成再結(jié)束main()主線程。
o.join(); //相當(dāng)于在main線程中同步o線程,o執(zhí)行完了,main線程才有執(zhí)行的機(jī)會(huì)
} catch (InterruptedException e) {
e.printStackTrace();
}
});
System.out.println(t.count);
}
}問(wèn)題六
寫(xiě)一個(gè)程序,在main線程中啟動(dòng)100個(gè)線程,100個(gè)線程完成后,主線程打印“完成”
package cn.thread;
import java.util.concurrent.CountDownLatch;
/**
* 寫(xiě)一個(gè)程序,在main線程中啟動(dòng)100個(gè)線程,100個(gè)線程完成后,主線程打印“完成”
*
* @author webrx [webrx@126.com]
* @version 1.0
* @since 16
*/
public class T12 {
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(100);
for (int i = 0; i < 100; i++) {
new Thread(() -> {
String tn = Thread.currentThread().getName();
System.out.printf("%s : 開(kāi)始執(zhí)行...%n", tn);
System.out.printf("%s : 執(zhí)行完成,程序結(jié)束。%n", tn);
latch.countDown();
}, "T" + i).start();
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("---------------------------------------");
System.out.println("100個(gè)線程執(zhí)行完了。");
String tn = Thread.currentThread().getName();
System.out.printf("%s : 執(zhí)行完成,程序結(jié)束。%n", tn);
}
}到此這篇關(guān)于高價(jià)值Java多線程面試題分析的文章就介紹到這了,更多相關(guān)Java 多線程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud實(shí)戰(zhàn)小貼士之Zuul的路徑匹配
這篇文章主要介紹了SpringCloud實(shí)戰(zhàn)小貼士之Zuul的路徑匹配,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Spring?AOP通知類型與實(shí)戰(zhàn)示例講解
Spring?AOP提供了五種通知類型:@Before、@After、@AfterReturning、@AfterThrowing和@Around,每種通知類型都有其特定的使用場(chǎng)景和實(shí)現(xiàn)方式,通過(guò)合理使用這些通知類型,可以實(shí)現(xiàn)各種橫切關(guān)注點(diǎn)的模塊化和解耦,感興趣的朋友跟隨小編一起看看吧2024-11-11
ReentrantReadWriteLock?讀寫(xiě)鎖分析總結(jié)
這篇文章主要介紹了ReentrantReadWriteLock 讀寫(xiě)鎖分析總結(jié),ReentranReadWriteLock中有兩把鎖,一把讀鎖,一把寫(xiě)鎖,關(guān)于這兩把鎖的介紹,需要的小伙伴可以參考一下2022-05-05
Java實(shí)現(xiàn)線程插隊(duì)的示例代碼
在編寫(xiě)多線程的業(yè)務(wù)時(shí),會(huì)遇到讓一個(gè)線程優(yōu)先于其他線程運(yùn)行的情況,除了可以設(shè)置線程的優(yōu)先級(jí)高于其他線程,還有更直接的方式:線程插隊(duì)。本文將用Java實(shí)現(xiàn)線程插隊(duì),需要的可以參考一下2022-08-08
springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼
阿里云推出了直傳,本文主要介紹了springboot+vue實(shí)現(xiàn)阿里云oss大文件分片上傳的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
SpringBoot實(shí)現(xiàn)本地上傳文件到resources目錄
Java后端項(xiàng)目上傳文件是一個(gè)很常見(jiàn)的需求,這篇文章主要為大家介紹了SpringBoot如何實(shí)現(xiàn)本地上傳文件到resources目錄永久保存下載,需要的可以參考一下2023-07-07

