Multithreading in Java

Source: Internet
Author: User

for Direct (extends) inheritance Thread class, the code's approximate framework is:

/*class class Name extends thread{Method 1; method 2;...public void Run () {//other code ...} Attribute 1; property 2; ...} */

let's look at a simple example:

Class Hello extends Thread {private String name; public hello () {} public hello (String name) {this.name = name;} Publ  IC void Run () {for (int i = 0; i < 4; i++) {System.out.println (THIS.name + "" + i);  }} public static void Main (string[] args) {Hello H1 = new Hello ("Thread A");  Hello h2 = new Hello ("thread B");  H1.run (); H2.run (); }}

"Run Results":

Thread A 0 thread a 1 thread a 2 thread a 3 thread B 0 thread B 1 thread B 2 thread B 3

we will find that these are executed sequentially, indicating that our call method is not correct, directly executing hello inside, run, and ordinary method call, should call is Start () method.

When we change the main function above to the following:

public static void Main (string[] args) {Hello H1 = new Hello ("Thread A");  Hello h2 = new Hello ("thread B");  H1.start (); H2.start (); }

Then run the program, the output of the possible results are as follows:

Thread A 0 thread B 0 thread A 1 thread B 1 thread A 2 thread a 3 thread B 2 thread B 3

because of the need to use CPU resources, so every time the results of the operation is basically different, hehe.

Note: Although we are calling here start () method, but actually called run () method.

Well: Why can't we just call Run () method?

The difference between the run () and start () methods in the thread class is as follows:

Run () method: Calls the Runnable object's run () method within this thread, and can be repeated multiple calls;

Start () method: Starts a thread, calls the Runnable object's Run () method, and cannot start a thread multiple times;

package thread.basic;public class threadtest { /**  *  observation Direct call to run () and start a thread with start () difference   *  *  @param  args  *  @throws  exception   */ public static void main (String[] args)  {  Thread  Thread = new threaddemo (); /*   The first     shows that:  run () is not a different call to other methods, The main method executes it sequentially and prints out the last sentence    thread.run ();    The second     indicates:  start () The,    method re-creates a thread after the main method execution ends, because the thread created by the start () method does not run to the end, so the main thread fails to exit, and then threads are executed. Note that the default thread created is the user thread (Non-daemon thread)    thread.start ();    The Third kind of    1, why not print out 100 sentences? Because we set the thread thread in order to Daemon (daemon) thread, only the daemon thread exists in the program, it can be exited, so it only prints seven sentences and exits the    2, when there is a daemon thread running in the Java Virtual machine, Java Virtual opportunity is turned off. When all regular threads have finished running, the virtual machine exits running regardless of where the,    daemon is running. So it's best that your daemon doesn't write business logic that will affect the program. Otherwise, you can't predict what's going to happen to the program.    thread.setdaemon (TRUE); &NBsp;  thread.start ();    fourth     user thread can be forced to kill by System.exit (0), so it only prints seven sentences   thread.start ();   system.out.println ("Main thread is over");   System.exit (3); */ } public static class threaddemo extends thread {    @Override   public void run ()  {   for  (int i  = 0; i < 100; i++)  {    system.out.println ("This  is a thread test " + i);    }  } }}

but Start method repeats the call, it appears java.lang.IllegalThreadStateException exception.

Multithreading in Java

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.