Java multi-Thread simple instance (Thread) and javathread
Package second; public class A extends Thread {public void run () {for (int I = 1; I <= 10; I ++) {for (int y = 1; y <= I; y ++) {System. out. print ("*");} System. out. println () ;}} public static void main (String args []) {A a1 = new A (); A a2 = new (); A a3 = new A (); a1.start (); a2.start (); a3.start ();}}
PS: If the output stars are messy, congratulations! Your multi-threaded instance is running successfully!
Java multithreading simple example
This can only blame your CPU for being too good !~ Processing speed is too fast!
Change the two cycles to 1000 or 10000.
Multithreading is actually a process where multiple threads are not executed together, but are processed in turn at one location.
Assume that it grabs the position again in 0.1 seconds. But if you finish what you want to do in 0.1 seconds, will you still grab a place?
A simple example of java Multithreading
For (int I = 0; I <10; I ++ ){
System. out. println (name + "run, I =" + I );
Thread. sleep (1)
}
The for loop can be changed in this way. Because the number of outputs is relatively small, when you start the second thread, the first thread has been running, so both outputs are output sequentially, either set the I to a large value or pause each Printing