Java Thread usage detailed _java

Source: Internet
Author: User

This article explains the Java Threading Technology in detail with the example, I believe it is helpful to understand the Java programming deeply. Specifically as follows:

A lot of people in learning Java have a certain understanding of the thread, and when we start to contact the Android development, it really is very important to discover how the thread is, this article on the use of Java threads to share the experience for everyone to reference.

First of all, we must distinguish between thread and process is not the same thing, what is the process? The process is like we need to execute the class file, and the thread is actually invoking CPU resources to run. A class file typically has only one process, but a thread can have many, and the execution of a thread is an asynchronous way of executing it.

One, how to open a second thread in the main function:

The sample code is as follows:

public class Thread_one {public
  static void Main (String [] args) {
    Run Run = new run ();
    Run.run ()//This is the invocation of the method, and the thread has a great difference to threads thread
    = new Thread (run);
    Thread.Start ();//start thread, call thread's run () method for
    (int i=1; i<=20; i++) {
      System.out.println ("Main thread I value:--------" +i);
    }
  }
}
Class Run implements runnable{

  @Override public
  void Run () {for
    (int i=1; i<=20; i++) {
      SYSTEM.OUT.PRINTLN ("Child Thread I Value:" +i);}
    }


The sleep method in the thread

The sample code is as follows:

public class Thread_sleep {
   * * * thread
   pause
  /public static void Main (String [] args) {
    Runone run = new Run One ();
    Thread thread = new thread (run); 
    Thread.Start ();
    try {
      Thread.Sleep (5000);
      Thread.Interrupt ();//In the execution of the thread
      //thread.stop ()//relative to the thread, stop is too rough, not recommended, generally when you need to force close child threads to use this method
    } catch ( Interruptedexception e) {
      e.printstacktrace ();
}}} Class Runone implements runnable{
  @Override public
  void Run () {for
    (int i=1; i<10; i++) {
      try {
   thread.sleep (1000);
        SYSTEM.OUT.PRINTLN ("-----" +new Date () + "-----");
      } catch (Interruptedexception e) {return
        ;//When a child thread is caught, close the child thread
      }}
  }}


Here is a special note, Thread.Interrupt (), can be the execution of the thread, relatively stop gentle so little, but not the best way to close the thread, here is a way to provide a:

public class Thread_stop {public
  static void Main (String [] args) {
    runthree run = new Runthree ();
    Thread th = new thread (run);
    Th.start ();
    try {
      Thread.Sleep (5000);
    } catch (Interruptedexception e) {
      e.printstacktrace ();
    }
    Run.setstop ();
  }
Class Runthree implements runnable{
  Boolean flag;
  @Override public
  Void Run () {
    flag = true;
    int i = 0;
    while (flag) {
      try {
        System.out.println ("Child thread----" + (i++));
        Thread.Sleep (1000);
      } catch (Interruptedexception e) {
        e.printstacktrace ();
  }}} public void Setstop () {
    flag = false;
  }  
}

Here's a brief introduction to the merging and surrendering in threads:

one, how to merge Threads , where the Join () method is called

The sample code is as follows:

public class Thread_join {
  *
   * * Merge
   thread
  /public static void Main (String [] args) {
    runtwo run = new Runt Wo ();
    Thread thread = new thread (run);
    Thread.Start ();
    try {
      thread.join ();//merge thread, which is equivalent to method call
    } catch (Interruptedexception e) {
      e.printstacktrace ();
    }
    for (int i=0; i<10; i++) {
      System.out.println ("Main thread:" +i);
}}} Class Runtwo implements runnable{

  @Override public
  void Run () {for
    (int i=0; i<10; i++) {
      SYSTEM.OUT.PRINTLN ("Child thread:----" +i);}}  


second, how to get the thread, where the thread's yield () method is called, as follows:

public class Thread_yield {

  /** out CPU
   * @param args
  /public static void main (string[] args) {
    th th = New Th ("AAA");
    Th.start ();
    for (int i = 0; i<=10; i++) {
      System.out.println ("main thread----" +i);
}} Class th extends thread{
  th () {}
  th (String s) {super (s);}

  @Override public
  Void Run () {for
    (int i = 0; i<=10; i++) {
      if (i%3!=0) {
        System.out.println ("Child threads" + i);
      } else{
        System.out.println ("Child thread i=" +i+ "thread Toggle");
        Yield ()///from thread successor can use this method}}}


Finally, let's share some questions about thread priority , as shown in the following code:

public class Thread_priority {
  /
   * * Priority set the priority of threads
   * Thread default priority is 5;thread's maximum priority is 10, minimum is 0
  * * public static void Main (String [] args) {
    T1 t1 = new T1 ();
    T2 t2 = new  T2 ();
    T1.start ();  
    T1.setpriority (thread.norm_priority+3);//Set Priority of T1
    T2.start ();
  }
Class T1 extends thread{

  @Override public
  void Run () {for
    (int i = 0; i<50; i++) {
      System.out.print ln ("Thread T1-----" +i);
}} Class T2 extends thread{

  @Override public
  void Run () {for
    (int i = 0; i<50; i++) {
      System.out.print ln ("Thread T2" +i);}}  


I believe that through the above code has been basic understanding of the threading mechanism in Java, I hope this article on the in-depth study of Java programming Help.

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.