There is one such interview question: Open a sub-thread and the main thread run concurrently, the child thread output 10 times and then the main thread output 100 times, so repeatedly 50 times. Let's look at the following code:
Package com.maso.test;/** * * @author Administrator * Two threads, where the first thread runs the output 10 times, the main threads run output 100 times, so repeat 50 times */public class T HREADTEST3 implements runnable{private static test test, @Overridepublic void Run () {for (int i=0; i<50; i++) {test.f1 (i) ;}} public static void Main (string[] args) {test = new test (), New Thread (New ThreadTest3 ()). Start (); for (int i=0; i<50; i++) {TEST.F2 (i);}} /** * Control and logic and data classification (this class is data) * @author Administrator * */static class test{private Boolean isf1 = true;/** * Output 10 times */public s ynchronized void F1 (int j) {if (!ISF1) {try {this.wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} for (int i=1; i<=10; i++) {System.out.println (Thread.CurrentThread (). GetName () + "first" + j + "Second round patrol, output" + i);} ISF1 = False;notify ();} /** * Output 100 times */public synchronized void F2 (int j) {if (ISF1) {try {this.wait ();} catch (Interruptedexception e) {E.printstack Trace ();}} for (int i=1; i<=100; i++) {System.out.println (Thread.CurrentThread (). GetName () + "first" + j + "Second round patrol, output" + i);} ISF1 = True;notifY ();}}} The above is judged by the IF statement, which does not seem to be a problem, in fact, it is not safe to do so because the thread is likely to be woken up during the wait, so we need to use the while statement. In addition, there are several points to note when using wait and notify:
1. When you call the Wait method and the Notity method of object, you must first obtain an object lock (which must be written in synchronized).
2. If the wait method for object is called, the thread will drop the object lock.
3, if A1, A2, A3 are all in object.wait (), then B call Object.notify () can only wake up A1, A2, A3 one (specific which is determined by the JVM)
4, Object.notifyall () can wake up all.
5, B When waking A, b if you still hold the object lock, you have to wait until B release the lock, a will have the opportunity to execute.
What's the difference between sleep and wait?
Sleep () does not release the object lock, and wait () releases the object lock. But wait () and sleep () can break the thread's suspend state through the interrupt () method, causing the thread to throw interruptedexception immediately. If thread A wants to end thread B immediately, you can call the interrupt method on the thread instance that corresponds to threads B. If thread B is wait/sleep/join at the moment, thread B will immediately throw interruptedexception, and return directly in catch () {} to safely end the thread. It is important to note that Interruptedexception is thrown from within the thread itself, not by the interrupt () method. When you call interrupt () on a thread, the thread does not throw interruptedexception at all if it is executing normal code. However, once the thread has entered wait ()/sleep ()/join (), the interruptedexception is immediately thrown.
Let's look at the thread life cycle:
Here's how to implement thread scheduling:
1, Sleep (): The thread is to let the thread sleep for a certain time, need to capture interruptedexception
2. Yield (): Pauses the current thread, letting the thread with the same rank priority run, without the same rank priority thread does not work. When it works, it will let the CPU run and go into a ready state.
3. Join (): Let a thread wait until the thread that calls the Join method finishes executing.
Look at the code:
public class ThreadTest4 implements runnable{private static int a = 0, @Overridepublic void Run () {for (int i=0; i<10; i+ +) {a++;}} public static void Main (string[] args) {new Thread (new ThreadTest4 ()). Start (); System.out.println (a);}}
Does this code output 10? The answer is no, because after the child thread is started, the value of a is immediately output, and the child thread has no action on a. Modify the following:
public class ThreadTest4 implements runnable{private static int a = 0, @Overridepublic void Run () {for (int i=0; i<10; i+ +) {a++;}} public static void Main (string[] args) throws Interruptedexception {thread t = new Thread (new ThreadTest4 ()); T.start (); T.J Oin (); System.out.println (a);}}
This output of the 10,join () method shows that it allows other threads to wait for the thread to finish executing.