Java Multithreading and synchronous implementation principle

Source: Internet
Author: User
Tags garbage collection reference sleep thread

A Implement multithreading

1. The false multithreading

Example 1:

public class TestThread
{
  int i=0, j=0;
  public void go(int flag)
  {
   while(true)
   {
    try{ Thread.sleep(100);
   }
   catch(InterruptedException e)
   {
    System.out.println("Interrupted");
   }
   if(flag==0) i++;
   System.out.println("i=" + i);
   }
   else
   {
    j++;
    System.out.println("j=" + j);
   }
  }
}
public static void main(String[] args)
{
  new TestThread().go(0);
  new TestThread().go(1);
}
}

The results of the above program are:

I=1

i=2

I=3

。。。

The result will always print out the value of I. Our intention is that when we call sleep () in the while loop, the other thread will start and print out the value of J, but that's not the result. About sleep () Why does not appear our expected results, in the following will be mentioned.

2. Implement multithreading

By inheriting class thread or implementing the Runnable interface, we can implement multithreading

2.1 Multithreading by inheriting class thread

There are two most important functions run () and start () in class thread.

1 the run () function must be overridden to place code that is to be processed in parallel in multiple threads into this function.

2 while the run () function implements parallel processing for multiple threads, we cannot call the run () function directly, but call the run () function by calling the start () function. When you call Start (), the start () function starts with multithreading-related initialization (which is why you can't call the run () function directly), and then calls the run () function.

Example 2:

public class TestThread extends Thread
{
  private static int threadCount = 0;
  private int threadNum = ++threadCount;
  private int i = 5;
  public void run()
  {
   while(true)
   {
    try
    {
     Thread.sleep(100); 
    }
    catch(InterruptedException e)
    {
     System.out.println("Interrupted");
    }
    System.out.println("Thread " + threadNum + " = " + i);
    if(--i==0) return;
   }
  }
  public static void main(String[] args)
  {
   for(int i=0; i<5; i++)
    new TestThread().start();
  }
}

The results of the operation are:

Thread 1 = 5

Thread 2 = 5

Thread 3 = 5

Thread 4 = 5

Thread 5 = 5

Thread 1 = 4

Thread 2 = 4

Thread 3 = 4

Thread 4 = 4

Thread 1 = 3

Thread 2 = 3

Thread 5 = 4

Thread 3 = 3

Thread 4 = 3

Thread 1 = 2

Thread 2 = 2

Thread 5 = 3

Thread 3 = 2

Thread 4 = 2

Thread 1 = 1

Thread 2 = 1

Thread 5 = 2

Thread 3 = 1

Thread 4 = 1

Thread 5 = 1

From the results, example 2 can achieve parallel processing of multithreading.

* *: In the above example, we only generate the thread object with new, and we do not use reference to record the resulting thread object. According to the garbage collection mechanism, when an object is not referenced by reference, it is reclaimed. But the garbage collection mechanism "does not set" for the thread object. Because each thread registers the action, even if we do not specify a reference point to this object when the thread object is generated, there is actually a reference point to that object somewhere, so the garbage collector cannot recycle them.

Related Article

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.