当前位置:网站首页>Thread - learning notes

Thread - learning notes

2022-06-25 15:27:00 BlackPenguin

Thread implementation

  1. Inherit extends Thread class
  2. Realization Runnable Interface
  3. Realization Callable Interface
  4. Thread pool : Is a thread queue , There are many threads stored inside , Can reduce the risk of creating 、 Time spent destroying threads

java Don't inherit more , But you can implement multiple interfaces , So use Runnable Interfaces are better than inheritance Thread Classes are more flexible , A class object implements multiple Runnable Interface .


producer / Consumer model - Thread communication

Include producers 、 consumer 、 And a buffer . producer -> The production data -> buffer -> Take the data -> consumer

The producer does not directly send the production data to consumers for use , Instead, the data is stored in the buffer first . Consumer fetches data from buffer .

advantage :

  1. There is little coupling between producers and consumers , It is a decoupling process
  2. Support concurrency , When the producer is faster , You don't have to wait for the consumer to finish processing the data , And waste time .
  3. It serves as a cache , The consumer didn't handle it in a timely manner , You can store unprocessed data in a buffer for later use
public class PCTest {
    
    public static void main(String[] args) {
    
        SynContainer synContainer = new SynContainer();

        Producer producer = new Producer(synContainer);
        Consumer consumer = new Consumer(synContainer);
        Consumer consumer1 = new Consumer(synContainer);

        new Thread(producer).start();
        new Thread(consumer).start();
        new Thread(consumer1).start();
    }
}

//  producer 
class Producer implements Runnable {
    

    SynContainer synContainer;

    public Producer(SynContainer synContainer) {
    
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
    
        for (int i = 0; i < 100; i++) {
    
            synContainer.push(new Chicken(i));
        }
    }
}

//  consumer 
class Consumer implements Runnable {
    

    SynContainer synContainer;

    public Consumer(SynContainer synContainer) {
    
        this.synContainer = synContainer;
    }

    @Override
    public void run() {
    
        for (int i = 0; i < 100; i++) {
    
            synContainer.pop();
        }
    }
}

//  product 
class Chicken {
    
    int id;

    public Chicken(int id) {
    
        this.id = id;
    }
}

//  buffer 
class SynContainer {
    
    //  You need a container 
    Chicken[] chickens = new Chicken[10];
    //  Container counter 
    int count = 0;

    //  Producers put in products 
    public synchronized void push(Chicken chicken) {
    
        if(count == chickens.length) {
    
            //  Notify consumers , Producer waiting 
            try {
    
                this.wait();
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }
        chickens[count] = chicken;
        System.out.println(" production id by " + chicken.id + ", count by " + count);
        count++;
        this.notify();
    }

    //  Consumers consume products 
    public synchronized Chicken pop() {
    
        while(count == 0) {
    
            try {
    
                this.wait();
            } catch (InterruptedException e) {
    
                e.printStackTrace();
            }
        }
        count--;
        Chicken chicken = chickens[count];
        System.out.println(" The consumption id by " + chicken.id + ", count by " + count);
        this.notify();

        return chicken;
    }
}



sleep And wait The difference between

  1. sleep() Belong to Thread;wait() Belong to Class, yes Object Class method ,Object.wait()
  2. sleep() A time will be passed in , When the time is up, it will return to the ready state ;wait() No incoming time , Get into “ Waiting indefinitely ”, Only pass notify() Wake up the .
  3. sleep The current thread is asleep , however No release lock , Other threads cannot use locked resources , Sleep under the lock . Because the thread sleeps CPU Free , So it Release CPU Executive power ;wait Release the lock , It can be understood as leaving the queue , Wait to be awakened before entering , Let others use resources first , also Release CPU Executive power .
  4. sleep Can be used anywhere ;wait Can only be used in synchronous methods or synchronous code blocks .
原网站

版权声明
本文为[BlackPenguin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200502589492.html