Problem:The sub-thread loops for 10 times, the main thread loops for 100 times, and then returns to the sub-thread loop for 10 times, and then returns to the main thread for another 100 times, so that the loop is 50 times.
Analysis:This is actually a very simple problem, that is, the sub-thread run, the main thread blocking ---> The sub-thread blocking, the main thread run, there are many ways to block the thread, the most common is sleep and wait. Of course, if you need to control it, use wait/notify. The sub-thread and the main thread operate on a Resource object, and the Resource object implements the operation object of the sub-thread and the main thread respectively by two methods.
The code is as follows:
Package treadgame;/*** thread interaction 1 * subthread loops 10 times, then the main thread loops 100 times, and then returns to the subthread loop 10 times, * then return to the main thread and loop for another 100 times, so that the loop is 50 times * @ author lcx **/public class ThreadExchange1 {public static void main (String [] args) {Resource res = new Resource (); new Thread (new Sub (res )). start (); for (int I = 1; I <= 50; I ++) {res. runMain () ;}} class Resource {boolean isSub = true; public synchronized void runMain () {if (isSub) {try {wait (); } Catch (InterruptedException e) {e. printStackTrace () ;}}for (int I = 1; I <= 100; I ++) System. out. println ("number of main thread loops" + I); isSub =! IsSub; policy ();} public synchronized void runSub () {if (! IsSub) {try {wait ();} catch (InterruptedException e) {e. printStackTrace () ;}}for (int I = 1; I <= 10; I ++) System. out. println ("subthread loop times" + I); isSub =! IsSub; policy () ;}} class Sub implements Runnable {Resource res; public Sub (Resource res) {this. res = res;} public void run () {for (int I = 1; I <= 50; I ++) {res. runSub ();}}}
Create two threads and run them alternately with the main thread.
Principle:
Pass the subclass object of the Runnable interface as the actual parameter to the constructor of the Thread class.
Why should we pass the subclass object of the Runnable interface to the constructor of Thread.
Because the custom run method belongs to a subclass object of the Runnable interface.
Therefore, let the thread specify the run method of the specified object. You must specify the object to which the run method belongs.
Call the start method of the Thread class to enable the Thread and call the run method of the subclass of the Runnable interface.
/* Create two threads and run them alternately with the main thread. */class E implements Runnable {public void run () {for (int x = 0; x <10; x ++) {System. out. println (Thread. currentThread (). getName () + "I am:" + x) ;}} public class Threade_15 {public static void main (String [] args) {E ee = new E (); thread t1 = new Thread (ee); Thread t2 = new Thread (ee); t1.start (); t2.start (); for (int x = 0; x <10; x ++) {System. out. println (Thread. currentThread (). getName () + "I am:" + x );}}}