In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java multithreaded face-to-face test case analysis of the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java multithreaded interview test case analysis article will have a harvest, let's take a look at it.
Question one
Thread An is executing synchronous methods in an object. Can thread B execute asynchronous methods in the same object at the same time?
Yes, the two threads need different resources to run and do not need to be preempted.
Case 1.
Package duoxiancheng2;/** * @ author yeqv * @ program A2 * @ Classname Ms1 * @ Date on 2022-2-7 19:08 * @ Email w16638771062@163.com * / public class Ms1 {/ A thread is executing synchronous methods in an object. Can thread B execute asynchronous methods in the same object at the same time? Object a = new Object (); public static void main (String [] args) {var t = new Ms1 (); new Thread (()-> t.a1 ()) .start (); / / A thread new Thread (()-> t.a2 ()) .start (); / / B thread} void A1 () {synchronized (a) {System.out.println (synchronization method) }} void a2 () {System.out.println ("asynchronous method");}}
Running result:
Question two
As above, can thread B execute another synchronization method in the same object at the same time?
No, two threads need a common resource, and the common resource is locked synchronously, which can only be occupied by one thread at a time.
Case 2.
Package duoxiancheng2;import java.util.concurrent.TimeUnit;/** * @ author yeqv * @ program A2 * @ Classname Ms2 * @ Date 19:25 on 2022-2-7 * @ Email w16638771062@163.com * / public class Ms2 {/ / same as above, can thread B execute another synchronization method in the same object at the same time? Object a = new Object (); public static void main (String [] args) {var t = new Ms2 (); new Thread (()-> t.a1 ()) .start (); / / A thread new Thread (()-> t.a2 ()) .start () / / B thread} void A1 () {synchronized (a) {System.out.println ("enter synchronization method 1"); try {TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("synchronization method 1 ends") } void a2 () {synchronized (a) {System.out.println ("enter synchronization method 2"); try {TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("synchronization method 2 ends") }}}
Running result:
Thread A runs first, taking up resources.
Thread B can not enter execution until thread A finishes running and releasing resources.
Thread B finishes execution
Question three
Will the lock be released when the thread throws an exception?
Yes, the thread releases resources immediately after an exception is thrown.
Case 3.
Package duoxiancheng2;import java.util.concurrent.TimeUnit;/** * @ author yeqv * @ program A2 * @ Classname Ms3 * @ Date on 2022-2-7 19:41 * @ Email w16638771062@163.com * / public class Ms3 {/ / will the lock be released if the thread throws an exception? Object a = new Object (); public static void main (String [] args) {var t = new Ms3 (); new Thread (()-> t.a1 ()). Start (); / / A thread new Thread (()-> t.a2 ()). Start (); / / B thread} void A1 () {int c = 3; int b Synchronized (a) {System.out.println ("enter synchronization method 1"); try {b = c / 0; System.out.println (b); TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("synchronization method 1 ends");}} void a2 () {synchronized (a) {System.out.println ("enter synchronization method 2"); try {TimeUnit.SECONDS.sleep (10);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("synchronization method 2 ends");}
Results: as soon as an exception occurs in the method, the resources are released immediately. Thread two starts execution.
Question 4
Write a program to prove that the AtomicInteger analogy is more efficient than synchronized
Synchronized is more efficient
Case one
Package duoxiancheng2;import java.util.concurrent.atomic.AtomicInteger;/** * @ author yeqv * @ program A2 * @ Classname Ms4 * @ Date on 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 sold one ticket, remaining d tickets. % n ", Thread.currentThread () .getName (), n.get ();} else {break;} * / synchronized (this) {if (num > 0) {num--; System.out.printf (" s sold one ticket, remaining d tickets. % n ", Thread.currentThread () .getName (), num);} else {break;} var b = System.currentTimeMillis (); System.out.println (b-a);}}
Synchronized results:
AtomicInteger results:
Question 5
Write a program to prove that multiple methods of the AtomXXX class do not constitute atomicity
Package demo16;import java.util.ArrayList;import java.util.List;import java.util.concurrent.atomic.AtomicInteger;/** * write a program to prove that multiple methods of class AtomXXX do not constitute atomicity * / public class T {AtomicInteger count = new AtomicInteger (0); void m () {for (int I = 0; I)
< 10000; i++) { if (count.get() < 100 && count.get() >= 0) {/ / if it is not locked, other threads will insert count.incrementAndGet ();} public static void main (String [] args) {T t = new T (); List 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) ->{the try {/ / join () method blocks the thread calling this method until thread t completes before the thread continues. It is usually used within the main () main thread to wait for other threads to finish before ending the main () main thread. O.join (); / / is equivalent to synchronizing o threads in main threads, o execution is finished, main threads have the opportunity to execute} catch (InterruptedException e) {e.printStackTrace ();}}); System.out.println (t.count);}} question 6
Write a program that starts 100threads in the main thread. After the 100thread finishes, the main thread prints "done".
Package cn.thread;import java.util.concurrent.CountDownLatch;/** * write a program that starts 100threads in the main thread. After the 100thread finishes, the main thread prints "done" * * @ 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: start execution.% n", tn); System.out.printf ("% s: execution completed, program finished. % n ", tn); latch.countDown ();}," T "+ I) .start ();} try {latch.await ();} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("- -"); System.out.println ("100 threads have finished executing.") ; String tn = Thread.currentThread (). GetName (); System.out.printf ("% s: execution completed, program completed.% n", tn);}} this is the end of the article on "case analysis of Java multithreaded interview questions". Thank you for reading! I believe you all have a certain understanding of the knowledge of "case analysis of Java multithreaded interview questions". If you still want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.