[BigData] multi-threaded Java foundation _

The so-called multi-threaded like a noodle snack street selling copper pots boss, the boss is like an operating system, if five customers at the same time to buy noodle, then, at this time the boss will be ready five copper pot while at the same time and then put water, spices, rice noodles, and cook for 5 individuals noodle, one by one if the cook, then cook the estimated two individuals noodle, behind three people to come to the boss refund.

The official explanation: multithreading (English: multithreading), refers to the technical realization of concurrent execution of multiple threads from a software or hardware

Four Steps to implement multithreading in Java:

(1) The need to use a multi-threaded execution logic manner, a writing implement runnable class (run method);

(2) create runnable achieve this object class;

(3) With this object is constructed of n runnable thread threads;

(4) these n-start thread (Thread.start ());

1. Multithreading case

Test categories: Demo1.java
Package cn.test.logan.day13;
 / ** 
 * multithreaded test class. 1 
 * @author QIN 
 * 
 * / 
public  class Demo1 the implements the Runnable { 

    @Override 
    public  void RUN () {
         for ( int I = 0; I <= 2 ; I ++ ) { 
            System.out.println ( "Demo1 threads -" + I + "**********" ); 
        }     
    } 
    

}

 Test categories: Demo2.java

Package cn.test.logan.day13;
 / ** 
 * Multithreaded Test class 2 
 * @author QIN 
 * 
 * / 
public  class Demo2 the implements the Runnable { 
        @Override 
        public  void RUN () {
             for ( int I = 0; I <=. 5 ; I ++ ) { 
                System.out.println ( "Demo2 threads" + I + "----------" ); 
            }     
        } 
}

 

Multi-threaded test categories: ThreadDemo .java

package cn.test.logan.day13;

public class ThreadDemo {
    
    public static void main(String[] args) {
        Demo1 demo1 = new Demo1();
        Demo2 demo2 = new Demo2();
        // 构造一个线程,指定要执行的逻辑
        Thread thread1 = new Thread(demo1);
        Thread thread2 = new Thread(demo1);
        Thread thread3 = new Thread(demo1);
        Thread thread4 = new Thread(demo2);
        Thread thread5 = new new the Thread (demo2); 
        
        // these five threads in a multithreaded manner start 
        thread1.start (); 
        thread2.start (); 
        thread3.start (); 
        thread4.start (); 
        thread5.start (); 
    } 
    
}

 

Results of the:

 

 2. objects pass data to Runnable

In practice, we sometimes need to transfer data to the dynamic object, at this time may be used to complete the constructor, following are the specific code.

Multi-threaded test code: ThreadDem.java

Package cn.test.logan.day13; 

public  class ThreadDemo { 
    
    public  static  void main (String [] args) { 
        Demo1 the demo1 = new new Demo1 ( "John Doe"); Demo1 demo12 = new new Demo1 ( "John Doe"); Demo1 demo13 = new new demo1 ( "Wang Wu" ); 
        demo2 demo2 = new new demo2 ();
         // construct a thread, specify the logic to perform 
        the thread Thread1 = new new the thread (demo1); 
        the thread Thread2 = new new the thread (demo12); 
        the thread thread3 = new new the Thread (demo13); 
        the Thread thread4= New new the Thread (demo2); 
        the Thread thread5 = new new the Thread (demo2); 
        
        // these five threads in a multithreaded manner start 
        thread1.start (); 
        thread2.start (); 
        thread3.start (); 
        thread4.start ( ); 
        thread5.start (); 
    } 
    
}

 

 

Test Class 1: Demo1.java

package cn.test.logan.day13;
/**
 * 多线程测试类1
 * @author QIN
 *
 */
public class Demo1 implements Runnable{
    private String name;
    
    public Demo1(String name) {
        super();
        this.name = name;
    }

    @Override
    public void run() {
        for(int i= 0;i<=2;i++) {
            System.out.println("Demo1中的线程-"+name);
        }    
    }
    

}

 

Test category 2: Demo2.java

Package cn.test.logan.day13;
 / ** 
 * Multithreaded Test class 2 
 * @author QIN 
 * 
 * / 
public  class Demo2 the implements the Runnable { 
        @Override 
        public  void RUN () {
             for ( int I = 0; I <=. 5 ; I ++ ) { 
                System.out.println ( "Demo2 threads" + I + "----------" ); 
            }     
        } 
}

 

 

Test Results:

Guess you like

Origin www.cnblogs.com/OliverQin/p/12149814.html