Multithreaded programming interview questions: create n threads, print m loop (a-z, a-z, case output) between reference links: write a Java multi-thread related interview questions such as: https://www.1024sou.com/article/228174.html Thread1-a, thread2-a, thread3-b... Loop twice. Create a specified number of threads to print m times in a loop that interleaves the threads to fetch data from two shared resources.Copy the code

This problem does not write out at the beginning, so follow the simple to complex to write out, and then optimize and upgrade.

Semaphore is used to print letters in multiple threads. Semaphore is used to print letters in multiple threads. Semaphore is used to print letters in multiple threads.

The simplest solution: N threads print the a-z interval and loop only once

public class ThreadInterleaving { private static final int N = 3; private static int count = 0; private static char a = 'a'; private static Semaphore[] semaphores = new Semaphore[N]; public static void main(String[] args) { try { new ThreadInterleaving().testAZTaskBasic(); } catch (InterruptedException e) { e.printStackTrace(); } } private void testAZTaskBasic() throws InterruptedException { for (int i = 0; i < N; i++) { semaphores[i] = new Semaphore(1); Semaphores [I].acquire(); if (I > 0) semaphores[I].acquire(); } for (int I = 0; i < N; i++) { Semaphore cur = semaphores[i]; Semaphore next = i == N - 1 ? semaphores[0] : semaphores[i + 1]; New Thread(new AZTaskBasic(cur, next), "Thread" + I + ": ").start(); }} /** * print the interval from a to Z and loop only once * Thread0: a * Thread1: b * Thread2: C * Thread0: d * Thread1: e * Thread2: f */ static class AZTaskBasic implements Runnable { private final Semaphore curSemaphore; private final Semaphore nextSemaphore; public AZTaskBasic(Semaphore curSemaphore, Semaphore nextSemaphore) { this.curSemaphore = curSemaphore; this.nextSemaphore = nextSemaphore; } @Override public void run() { while (true) { try { curSemaphore.acquire(); if (count >= 26) break; System.out.println(Thread.currentThread().getName() + (char) (a + count++)); } catch (InterruptedException e) { e.printStackTrace(); } finally { nextSemaphore.release(); } } } } }Copy the code

Print result:

Thread0: A Thread1: B Thread2: C Thread0: D Thread1: E Thread2: F Thread0: g Thread1: H Thread2: I Thread0: J Thread1: K Thread2: d Thread1: E Thread2: F Thread0: G Thread1: H Thread2: I Thread0: J Thread1: K Thread2: L Thread0: M Thread1: N Thread2: O Thread0: P Thread1: Q Thread2: R Thread0: S Thread1: T Thread2: U Thread0: V Thread1: W Thread2: X Thread0: y Thread1: zCopy the code

The simplest solution is 2: N threads print the a-Z interval and loop twice

public class ThreadInterleaving { private static final int N = 3; private static int count = 0; private static char a = 'a'; private static Semaphore[] semaphores = new Semaphore[N]; public static void main(String[] args) { try { new ThreadInterleaving().testAZTaskBasicPrintTwice(); } catch (InterruptedException e) { e.printStackTrace(); } } private void testAZTaskBasicPrintTwice() throws InterruptedException { for (int i = 0; i < N; i++) { semaphores[i] = new Semaphore(1); if (i > 0) semaphores[i].acquire(); } for (int i = 0; i < N; i++) { Semaphore cur = semaphores[i]; Semaphore next = i == N - 1 ? semaphores[0] : semaphores[i + 1]; New Thread(new AZTaskBasicPrintTwice(cur, next), "Thread" + I + ": ").start(); }} /** * */ static class AZTaskBasicPrintTwice implements Runnable {private final Semaphore curSemaphore; private final Semaphore nextSemaphore; public AZTaskBasicPrintTwice(Semaphore curSemaphore, Semaphore nextSemaphore) { this.curSemaphore = curSemaphore; this.nextSemaphore = nextSemaphore; } @Override public void run() { while (true) { try { curSemaphore.acquire(); if (count >= 26) { time++; if (time >= REPEAT_TIME) break; count = 0; } System.out.println(Thread.currentThread().getName() + (char) (a + count++)); } catch (InterruptedException e) { e.printStackTrace(); } finally { nextSemaphore.release(); } } } } }Copy the code

Print result:

Thread0: A Thread1: B Thread2: C Thread0: D Thread1: E Thread2: F Thread0: g Thread1: H Thread2: I Thread0: J Thread1: K Thread2: d Thread1: E Thread2: F Thread0: G Thread1: H Thread2: I Thread0: J Thread1: K Thread2: L Thread0: M Thread1: N Thread2: O Thread0: P Thread1: Q Thread2: R Thread0: S Thread1: T Thread2: U Thread0: V Thread1: W Thread2: X Thread0: Y Thread1: Z Thread2: A Thread0: B Thread1: C Thread2: D Thread0: E Thread1: F Thread2: G Thread0: H Thread1: I Thread2: J Thread0: K Thread1: L Thread2: M Thread0: N Thread1: O Thread2: P Thread0: Q Thread1: R Thread2: S Thread0: T Thread1: U Thread2: J Thread0: K Thread1: L Thread2: M Thread0: N Thread1: O Thread2: P Thread0: Q Thread1: R Thread2: S Thread1: U Thread2: V Thread0: W Thread1: X Thread2: Y Thread0: ZCopy the code

Primary: N threads print aBcDeFg.. YZ case interval output, loop only once

public class ThreadInterleaving { private static final int N = 3; private static int count = 0; private static char a = 'a'; private static Semaphore[] semaphores = new Semaphore[N]; public static void main(String[] args) { try { new ThreadInterleaving().testAZTaskLowerHigherCase(); } catch (InterruptedException e) { e.printStackTrace(); } } private void testAZTaskLowerHigherCase() throws InterruptedException { for (int i = 0; i < N; i++) { semaphores[i] = new Semaphore(1); if (i > 0) semaphores[i].acquire(); } for (int i = 0; i < N; i++) { Semaphore cur = semaphores[i]; Semaphore next = i == N - 1 ? semaphores[0] : semaphores[i + 1]; New Thread(new AZTaskLowerHigherCase(cur, Next), "Thread" + I + ": ").start(); }} /** * Primary: N threads print aBcDeFg.. */ static class AZTaskLowerHigherCase implements Runnable {private Final Semaphore curSemaphore; private final Semaphore nextSemaphore; public AZTaskLowerHigherCase(Semaphore curSemaphore, Semaphore nextSemaphore) { this.curSemaphore = curSemaphore; this.nextSemaphore = nextSemaphore; } @Override public void run() { while (true) { try { curSemaphore.acquire(); if (count >= 26) break; if (count % 2 == 0) { System.out.println(Thread.currentThread().getName() + (char) (a + count)); } else { System.out.println(Thread.currentThread().getName() + (char) (A + count)); } count++; } catch (InterruptedException e) { e.printStackTrace(); } finally { nextSemaphore.release(); } } } } }Copy the code

Print result:

Thread0: A Thread1: B Thread2: C Thread0: D Thread1: E Thread2: F Thread0: g Thread1: H Thread2: I Thread0: J Thread1: K Thread2: D Thread1: E Thread2: F Thread0: G Thread1: H Thread2: I Thread0: J Thread1: K Thread2: L Thread0: M Thread1: N Thread2: O Thread0: P Thread1: Q Thread2: R Thread0: S Thread1: T Thread2: U Thread0: V Thread1: W Thread2: X Thread0: y Thread1: ZCopy the code

Final solution: Print aA.. ZZ case interval output, loop 2 times

That is, create n threads and loop m times (A-z, A-z, case interval output)

public class ThreadInterleaving { private static final int N = 3; private static final int REPEAT_TIME = 2; private static int time = 0; private static int count = 0; private static final int MAX_LETTER_COUNT_PER_ROUND = 26 * 2; private static char A = 'A'; private static char a = 'a'; private static int lowerCaseIndex = 0; private static int higherCaseIndex = 0; private static Semaphore[] semaphores = new Semaphore[N]; public static void main(String[] args) { try { new ThreadInterleaving().testAZTask(); } catch (InterruptedException e) { e.printStackTrace(); } } private void testAZTask() throws InterruptedException { for (int i = 0; i < N; i++) { semaphores[i] = new Semaphore(1); if (i > 0) semaphores[i].acquire(); } for (int i = 0; i < N; i++) { Semaphore cur = semaphores[i]; Semaphore next = i == N - 1 ? semaphores[0] : semaphores[i + 1]; New AZTask(cur, next), "Thread" + I + ": ").start(); }} /** * Prints aA.. */ static class AZTask implements Runnable {private final Semaphore curSemaphore; private final Semaphore nextSemaphore; public AZTask(Semaphore curSemaphore, Semaphore nextSemaphore) { this.curSemaphore = curSemaphore; this.nextSemaphore = nextSemaphore; } @Override public void run() { while (true) { try { curSemaphore.acquire(); if (count >= MAX_LETTER_COUNT_PER_ROUND) { time++; if (time >= REPEAT_TIME) break; count = 0; lowerCaseIndex = 0; higherCaseIndex = 0; } if (count % 2 == 0) { System.out.println(Thread.currentThread().getName() + (char) (a + lowerCaseIndex++)); } else { System.out.println(Thread.currentThread().getName() + (char) (A + higherCaseIndex++)); } count++; } catch (InterruptedException e) { e.printStackTrace(); } finally { nextSemaphore.release(); } } } } }Copy the code

Print result:

Thread0: A Thread1: A Thread2: B Thread0: B Thread1: C Thread2: C Thread0: D Thread1: D Thread2: E Thread0: E Thread1: F Thread2: F Thread0: G Thread1: G Thread2: H Thread0: H Thread1: I Thread2: I Thread0: J Thread1: J Thread2: K Thread0: K Thread1: L Thread2: L Thread0: M Thread1: M Thread2: N Thread0: N Thread1: O Thread2: O Thread0: P Thread1: P Thread2: Q Thread0: Q Thread1: R Thread2: L Thread1: M Thread1: M Thread2: N Thread2: O Thread0: P Thread1: P Thread2: Q Thread1: R Thread2: R Thread0: S Thread1: S Thread2: T Thread0: T Thread1: U Thread2: U Thread0: V Thread1: V Thread2: W Thread0: W Thread1: x Thread2: X Thread0: y Thread1: Y Thread2: Z Thread0: Z Thread1: A Thread2: A Thread0: B Thread1: B Thread2: C Thread0: C Thread1: D Thread2: D Thread0: E Thread1: E Thread2: F Thread0: F Thread1: G Thread2: G Thread0: H Thread1: H Thread2: I Thread0: I Thread1: J Thread2: J Thread0: K Thread1: K Thread2: L Thread0: L Thread1: M Thread2: M Thread0: N Thread1: N Thread2: O Thread0: O Thread1: P Thread2: J Thread1: K Thread1: K Thread2: L Thread1: M Thread2: M Thread0: N Thread1: O Thread1: P Thread2: P Thread0: Q Thread1: Q Thread2: R Thread0: R Thread1: S Thread2: S Thread0: T Thread1: T Thread2: U Thread0: U Thread1: V Thread2: V Thread0: W Thread1: W Thread2: X Thread0: X Thread1: Y Thread2: Y Thread0: Z Thread1: ZCopy the code