java - thread - multithreading

https://www.cnblogs.com/clamp7724/p/11648308.html

Use the built-in Vector java security thread on the basis of multi-threading.

Try to lock the handle

 

Thread lock:

synchronized 

using synchronized modification methods, so call this method when this method is locked, only the current object after use in order to allow the release of other objects to use.
This may not add multiple objects simultaneously access methods, resulting in threading issues (such as shipping method, A run to the method of determining whether there are goods, as well as cargo judge, B method then just run away goods, A error will start getting goods)

public void the synchronized method name () {...}
or
synchronized (locked objects needed) {...} unlock after executing the code

Thread control:
the wait:
the Object.wait () allow the implementation of the Object thread into a wait state until the cpu allocate resources again (as long as the thread does not die out or error, cpu free time will automatically allocate resources to execute it), pay attention here operation of the object is threads, multiple single-threaded operating the same object, one thread does not affect other threads waiting to enter the operating object.
If you write in a this.wait method (); into a wait state is currently operating the thread object, not the current class of objects. For example, A calls B method, this method has a this.wait (), to perform there this thread broken (no longer continue down), wake-up wait before proceeding. It does not affect the call to B to C.
All threads into the wait () --- "state of suspended animation.
Need to find time to prevent suspended animation thread wake

Object.notify:
wake up a thread in which the current object (cpu priority is determined, or in advance (int priority) Set the priority with object.setPriority, priority 1 to 10, the highest 10 )

the Object.notifyAll:
All threads wake objects.

Production of consumer model:
it was put to the warehouse, some people take from the warehouse, a warehouse operation is the same people at the same time.

The main function

package multi_thread;

public class MultiThreadTest {
    public static void main(String[] args) {
        C1 the Customer = new new the Customer ( "Consumer 1" );
        C2 the Customer = new new the Customer ( "Consumer 2" );
        C3 the Customer = new new the Customer ( "Consumer 3" );
        C4 the Customer = new new the Customer ( "Consumer 4" );

        P1 productor = new new productor ( "Producer 1" );
        P2 productor = new new productor ( "Producer 2" );
         // high priority of some producers, production first and then consume 
        p1.setPriority (10 );
        p2.setPriority ( 10 );


        // initial 10 products, there are four consumers buy 20 per person, two producers produce 25 per person, which means there will be at least 20 times buy (consumption rate> production speed, not enough time to produce You may not buy)

        c1.start();
        c2.start();
        c3.start();
        c4.start();
        p1.start();
        p2.start();


    }
}

 

 

consumer

package multi_thread;

public  class the Customer the extends the Thread { // consumers to buy goods to take 
    Private String name;
     public the Customer (String name) {
         the this .name = name;
    }

    public  void RUN () {
         for ( int I = 0; I <20 is; I ++) {   // Assume that each consumer products require 20 
            buyGood ();
        }
    }

    private void buyGood(){
        Goods g = Goods.getInstance();
        String s = g.removeGood();

        // another way lock multithreaded added synchronized (lock object) in the outer section of the code {}
         // This method of locking and achieve the same effect
 //         synchronized (G) {
 //             S = G. removeGood ();
 //         }

        if(s != null){
            System.out.println (name + "bought" + S);
        }
        else{
            System.out.println (name + "found out of stock it!" );
        }
    }
}

 

Producers

package multi_thread;

public  class Productor the extends the Thread { // Manufacturer, increasing cargo 
    Private String name;
     public Productor (String name) {
         the this .name = name;
    }

    public  void RUN () {
         for ( int I = 0; I <30; I ++) {   // Assume that each stop 30 products producers to produce, just to meet consumer 
            productGood ();
        }
    }

    private void productGood(){
        Goods g = Goods.getInstance();
        String s = g.addGood();
        System.out.println (name + "produced" + S);

    }
}

 

Cargo warehouse

package multi_thread;

import java.util.ArrayList;

public  class Goods { // cargo warehouse, the goods represented by String

    Private  int NUM =. 1; // used to distinguish goods 
    Private  static Goods Goods = new new Goods ();
     Private the ArrayList <String> = goodList new new the ArrayList <> ();   // with the goods on behalf String, create a new class of bother. . .

    {
        for(int i = 0; i < 20; i++){
            addGood();
        }
    }
    // constructor, add 20 initial cargo

    private Goods(){
    }

    public static Goods getInstance(){
        return goods;
    }

    // produce a cargo 
    public  synchronized String addGood () {   // use synchronized locking method, so call this method when, Goods objects are locked, after the implementation to release. 

        goodList.add ( "cargo" + NUM);
        NUM ++ ;
         return "goods" + NUM; // returns the name of the production of goods 
    }

    // consume a cargo 
    public  the synchronized String removeGood () {
         IF (goodList.size ()> 0 ) {
            S String = goodList.remove (0 );
             return S; // returns the name of consumer goods; 
        }
         the else {
             the try {
                 // the wait is not a cargo of threads, but where the currently executing thread object to this method (only consumer calls this method) 
                System.out.println ( "cargo was found to" + num + "when out of stock it, and so it does not move in there" );

                // not added this.notifyAll (); in the case of:
                 // found this sentence run four times, four of consumers did not buy things (because consumer spending rate of> producers to produce speed, not enough time production sold out), only two producers still in production.
                // program card no longer continue here, known as: state of suspended animation.
                // In order to prevent suspended animation when the thread enters a wait state, wake up other threads.
                //

                // the Notify 
                the this .notifyAll ();   // ---- coupled with the phrase, the thread can be completed properly. After some consumers find out of stock, over time will try again over the purchase (other consumers found out of stock so that he woke up) 
                the this .wait ();
            } catch (Exception e) {
                e.printStackTrace ();
            }
            return null;
        }
    }


}

 

 

 

Guess you like

Origin www.cnblogs.com/clamp7724/p/11649719.html