您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java多线程面试题详解

2025/8/19 19:24:50发布19次查看
问题一a线程正在执行一个对象中的同步方法,b线程是否可以同时执行同一个对象中的非同步方法?
可以,两个线程运行所需资源不同,不需要抢占。
案例一、
package duoxiancheng2;/** * @author yeqv * @program a2 * @classname ms1 * @date 2022/2/7 19:08 * @email w16638771062@163.com */public class ms1 { //a线程正在执行一个对象中的同步方法,b线程是否可以同时执行同一个对象中的非同步方法? 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("非同步方法"); }}
运行结果:
问题二同上,b线程是否可以同时执行同一个对象中的另一个同步方法?
不可以,两个线程执行需要一个共同资源,共同资源加了同步锁,同一时刻只能一个线程占用。
案例二、
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线程是否可以同时执行同一个对象中的另一个同步方法? 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("进入同步方法1"); try { timeunit.seconds.sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("同步方法1结束"); } } void a2() { synchronized (a) { system.out.println("进入同步方法2"); try { timeunit.seconds.sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("同步方法2结束"); } }}
运行结果:
线程a先运行,占用资源。
等线程a运行完释放资源后,线程b才可以进入执行
线程b执行完
问题三线程抛出异常会释放锁吗?
会,线程出现异常抛出后立刻释放资源。
案例三、
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 { //线程抛出异常会释放锁吗? 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("进入同步方法1"); try { b = c / 0; system.out.println(b); timeunit.seconds.sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("同步方法1结束"); } } void a2() { synchronized (a) { system.out.println("进入同步方法2"); try { timeunit.seconds.sleep(10); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("同步方法2结束"); } }}
结果: 方法一出现异常,立刻释放资源。线程二开始执行
问题四写一个程序,证明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结果:
atomicinteger结果:
问题五写一个程序证明atomxxx类的多个方法并不构成原子性
package demo16;import java.util.arraylist;import java.util.list;import java.util.concurrent.atomic.atomicinteger;/** * 写一个程序证明atomxxx类的多个方法并不构成原子性 */public class t { atomicinteger count = new atomicinteger(0); void m() { for (int i = 0; i < 10000; i++) { if (count.get() < 100 && count.get() >= 0) { //如果未加锁,之间还会有其他线程插进来 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()方法阻塞调用此方法的线程,直到线程t完成,此线程再继续。通常用于在main()主线程内,等待其它线程完成再结束main()主线程。 o.join(); //相当于在main线程中同步o线程,o执行完了,main线程才有执行的机会 } catch (interruptedexception e) { e.printstacktrace(); } }); system.out.println(t.count); }}
问题六写一个程序,在main线程中启动100个线程,100个线程完成后,主线程打印“完成”
package cn.thread;import java.util.concurrent.countdownlatch;/** * 写一个程序,在main线程中启动100个线程,100个线程完成后,主线程打印“完成” * * @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 : 开始执行...%n", tn); system.out.printf("%s : 执行完成,程序结束。%n", tn); latch.countdown(); }, "t" + i).start(); } try { latch.await(); } catch (interruptedexception e) { e.printstacktrace(); } system.out.println("---------------------------------------"); system.out.println("100个线程执行完了。"); string tn = thread.currentthread().getname(); system.out.printf("%s : 执行完成,程序结束。%n", tn); }}
以上就是java多线程面试题详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product