The content of this interview question is that two threads are required to print alternately and print out " 12a34b56c78d910e1112f1314g1516h1718i1920j2122k2324l2526m2728n2930o3132p3334q3536r3738s3940t4142u4344v4546w4748x4950y5152z "。
One thread prints only the number, another thread prints only the letters, the thread that prints the number prints two digits, the printed letter prints a letter, and then alternates until all the letters are printed, and the number prints to 52.
The idea is that the thread is running alternately, the key to the alternate operation is that one thread performs a cycle, hangs immediately, notifies another thread to execute, and the other thread executes, and immediately hangs, then notifies the previous thread. All two threads need to be suspended and notified, but two threads are independent of each other, so it is not appropriate to use Object monitor locks because the object monitor hangs and notifications are no different, and it is possible to activate this thread immediately after it is suspended, and the thread that needs to be activated is still suspended ... So here to use Reentrantlock and two condition, not to say more to see the code.
Thread A is responsible for printing numbers
Class Threada extends thread{
A lock and two condition
Private lock lock;
Private Condition C1;
Private Condition C2;
Construct methods to inject these references
Public Threada (Lock lock,condition c1,condition C2) {
This.lock=lock;
THIS.C1=C1;
THIS.C2=C2;
}
Thread Start
public void Run () {
try {
Lock.lock ();//Lock
Cycle 52 times
for (int i=1;i<=52;i++) {
I can print when I am odd, print two times
if (i%2!=0) {
System.out.print (i+ "" + (i+1));
C2.signal ()//notify C2 This condition begin to run
}else{
C1.await (); C1 hangs when//i is an even number
}
}
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
finally {
Lock.unlock ();
}
}
}
Class Threadb extends thread{
Private lock lock;
Private Condition C1;
Private Condition C2;
Public threadb (Lock lock,condition c1,condition C2) {
This.lock=lock;
THIS.C1=C1;
THIS.C2=C2;
}
public void Run () {
try {
Lock.lock ();
Char c= ' A '///define char variable as print variable
for (int i=0;i<51;i++) {//Loop 51 times, because traversing 26 letters and inserting two digits between each letter, you need to cycle 51 times, I was debugging out, initially I do not know need 51 times ... Char is not used as a loop variable because it is not printed when the odd number of cycles are involved, so char will lose printing if it skips over the odd number of times as a loop variable.
if (i%2==0) {//occasionally cycles, print C and increase, notify C1 this condition can run.
System.out.print (c + +);
C1.signal ();
When the}else{//cycle is odd several times, C2 this condition hangs.
C2.await ();
}
}
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
finally {
Lock.unlock ();
}
}
}
This is a two thread class, see Main method.
public static void Main (string[] args) throws Interruptedexception {
Lock lock=new Reentrantlock ();
Condition c1=lock.newcondition ();
Condition c2=lock.newcondition ();
Threada a=new Threada (LOCK,C1,C2);
THREADB b=new threadb (LOCK,C1,C2);
A.start ();
Thread.Sleep (50);
B.start ();
}
It's not hard, thread A starts first, then thread B starts again. The results are correct.
I write is not necessarily the best, there must be more excellent solution, welcome to correct.