Compare (comparison of two ways) Java multi-threaded implementation _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

Look at the operating status of each stage of the java runtime thread

Hi learning network

A thread is a physical process, is the basic unit of independent scheduling and dispatch system, the thread does not own its own system resources, it has only a little in the operation of essential resources, but it can be shared with other processes belong to the same process of thread We have all the resources. A thread can create and undo another thread can execute concurrently across multiple threads in the same process. Due to the interaction between threads, resulting in the thread showing a discontinuity in the operation.

In the introduction of the thread Caozuojitong usually are the process as the basic unit of resource allocation, and the thread as a basic unit operate independently and scheduled separately. Since the thread is smaller than the process, basically do not have the system resources, so it's scheduled to pay for the cost would be far less, the degree of improvement can be more efficient within the system across multiple programs concurrently, thereby significantly improving system resources utilization and throughput.

A thread is a single sequential program control flow. Simultaneously run multiple threads to complete different jobs in a single program, called multi-threading.

Multithreading is mainly to save CPU time, the use of play, running thread in the need to use the CPU and memory resources of the computer.

Multithreading is to synchronize multiple tasks, not to improve operational efficiency, but to improve the efficiency of resource use to improve the efficiency of the system. Threads are implemented at the same time need to complete a number of tasks of the time.

java multi-threaded There are two ways

1, inheritance Thread class

2, implement Runnable

Both methods have in common:

Regardless of which method is used, it must be (if it is Thead subclass to use it themselves) to produce thread with Thread, and then call start () method.

Different points of two ways:

1, inheritance Thread class has a drawback is that single inheritance, to achieve Runnable interface is made up for its shortcomings, you can achieve multiple inheritance

2, inheritance Thread class must produce if Runnable instance of an object, it is necessary to generate a plurality of Runnable instances of objects, and then use the Thread generate multiple threads; implemented the Runnable interface, only need to create an instance of this class implementation, then use this one instance Object generate multiple threads. That is, to achieve the sharing of resources

It is proposed that the second method based on the above two points

The following example of any use

Program 1:

com.dr.runnable1 Package; 
// a class can inherit the Thread class, the class of such multithreaded 
class the MyThread the extends Thread 
{ 
 Private String name; 
 public the MyThread (String name) 
 { 
  this.name = name; 
 } 
 // if to use the multi-threaded, the body must have a method of 
 public void RUN () 
 { 
  // print output 
  for (int I = 0; I <10; I ++) 
  { 
   System.out.println (this.name + "---- -> run ,,,, "); 
  } 
 } 
} 
public class Demo1 { 
 public static void main (String args []) 
 { 
// The first method 
   Runnable r1 = new MyThread (" thread A "); 
  the Runnable new new R2 = MyThread ( "thread B"); 
  the Runnable R3 = new new MyThread ( "thread C");
  Thread t1=new Thread(r1);
  Thread t2=new Thread(r2); 
  the Thread new new the Thread T3 = (R3); 
  t1.start (); 
  t2.start (); 
  t3.start (); 
 // mt1.run (); // a thread of execution, using start method 
// mt2.run (); 
// mt3.run (); 
// second method 
// MyThread mt1 = new MyThread ( "thread A"); 
// = the MyThread new new MT2 the MyThread ( "thread B" ); 
// MyThread MT3 = new new MyThread ( "thread C"); 
// mt1.start (); 
// mt1.start (); // thread can only be started once 
// mt2.start (); 
// MT3 .start (); 
 } 
}

Operating results of the program are:

Hi learning network

This is a Thread class inheritance, the first instance of a method for generating a plurality of objects Runnable, then generates a plurality of threads by Thread

The second method, because this class has inherited the Thread class, so you can directly use it itself produces multiple threads

Program 2:

package com.dr.runnable1;
class MyThread1 implements Runnable
{
 private int ticket=10;
 public void run()
 {
  for(int i=0;i<500;i++)
  {
   if(this.ticket>0)
   {
    System.out.println("卖票----->"+(this.ticket--));
   }
  }
 }
}
public class Demo2 {
 public static void main(String args[])
 {
   MyThread1 mt=new MyThread1();
  Thread t1=new Thread(mt);
  Thread t2=new Thread(mt);
  Thread t3=new Thread(mt);
  t1.start();
  t2.start();
  t3.start();
 } 
} 

Result of the program:

Hi learning network

This program is a Runnable, producing an object instance of a class, then generating a plurality of threads using Thread.

The original address is: http: //www.piaodoo.com/thread-13260-1-1.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093680.html