Java multi-Thread simple instance (Thread) and javathread

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.