Java Note thread Mode 1 start thread

Source: Internet
Author: User

* Requirements: We want to implement multi-threaded program.
* How to achieve it?
* Because threads are dependent on processes, we should create a process first.
* and the process is created by the system, so we should go to invoke the system function to create a process.
* Java is not directly called system functions, so we have no way to directly implement multithreaded programs.
* But what? Java can go to call C + + written programs to implement multithreaded programs.
* The system function creation process is called by C + + and then by Java to invoke such things,
* Then provide some classes for us to use. We can implement multithreaded programs.
* So what are the classes provided by Java?
* Thread
* By looking at the API, we know there are 2 ways to implement multithreaded programs.
*
* Way 1: Inherit the Thread class.
* Steps
* A: Custom class Mythread inherits the thread class.
* B:mythread class inside rewrite run ()?
* Why is the run () method?
* C: Create object
* D: Start thread

What is the difference between run () and start ()?
Run (): Just encapsulate the code executed by the thread, the direct call is the normal method
Start (): Starts the thread first, and then the JVM calls the thread's run () method.

1  Public classMythreaddemo {2      Public Static voidMain (string[] args) {3         //To create a thread object4         //MyThread my = new MyThread ();5         // //Start Thread6         //My.run ();7         //My.run ();8         //Why is the call to the run () method a single thread?9         //because the run () method directly calls is actually equivalent to the normal method call, so you see the effect of a single threadTen         //To see the effects of multithreading, you must say another method: Start () One         //Interview Question: What is the difference between run () and start ()? A         //run (): Just encapsulate the code executed by the thread, the direct call is the normal method -         //start (): Starts the thread first, and then the JVM calls the thread's run () method.  -         //MyThread my = new MyThread (); the         //My.start (); -         // //illegalthreadstateexception: Illegal thread-state exception -         // //for what? Because this is equivalent to the My thread was called two times. Instead of two threads to start.  -         //My.start (); +  -         //Create two Thread objects +MyThread my1 =NewMyThread (); AMyThread My2 =NewMyThread (); at  - My1.start (); - My2.start (); -     } - } -  Public classMyThread extends Thread { in  - @Override to      Public voidrun () { +         //Write your own code -         //System.out.println ("Good study, day Up"); the         //In general, code that is executed by threads must be time-consuming. So we use loops to improve *          for(intx =0; X < $; X + +) { $System. out. println (x);Panax Notoginseng         } -     } the  +}
View Code

Java Note thread Mode 1 start thread

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.