Java basic column - Thread

Process overview

A running program is a process

Single thread will not have security problems, but the efficiency is worrying

  • Time-sharing scheduling: average CPU usage per thread
  • Preemptive scheduling: Threads with different priorities can

Thread subclasses implement threads

  1. Inherit to the Thread class and override the run() of the Thread method
  2. Put the code to be executed in run()
  3. It is illegal to create a subclass object, call start(), and start a thread multiple times
  4. Execution is random

​Because the问什么要继承Thread类: Thread class is a thread class, a class that inherits from Thread can provide a template for the code block written in run(). So run is to provide the code to run, and start() is to start the thread.

线程的内存:​The memory栈内存都是线程私有的

Thread method

public String getName():这是Thread的方法,并不是子类的方法

public static Thread currentThread()

public void setName(String name)用子类对象去调用这个方法

public void sleep(long second)==This method cannot throw exceptions, because the parent class Thread cannot throw, so the subclass cannot throw ==

Runnable

  • A class that implements the Runnable interface
  • Override the run method
  • Create Thread class object
  • The constructor passes the implementation class and the interface implementation class of Runnable
  • call strat()
public subRunnable implements Runnable{
    public void run(){
        
    }
}
public static void main(String[] args){
    SubRunnable sr = new SubRunnable();
  	Thread th = new Thread(sr);
  	th.start();
}
  1. Interfaces address the limitations of single inheritance
  2. Separate the task object from the thread object

Anonymous inner class implements multithreading

//匿名内部类是没有子类的
//继承方式 xxxclass extends Thread{public void run()}
//直接new Thread,这个匿名内部类就是Thread的子类
new Thread(){
    public void run(){
        
    }.start();
}
//匿名内部类实现类接口引用
Runnable r = new Runnable(){
    public void run(){
        
    };
  	new Thread(r).start;
}

new Thread(new Runnable(){
    public void run(){
        
    }
}).start();

Thread Pool Principle

The thread pool is a container used to store threads. When the thread is not tried again, the thread will be recycled but the thread will not be closed. This thread can be reused, saving the memory overhead of repeatedly creating and destroying threads. sun company provides thread pool API after JKD1.5

The thread pool container is 线程工厂produced by

|--Utils
	|--Executors
	
public static ExecutorService newFixedThreadPool(int nThreads)
  
ExecutorService:线程池接口
Future<?> submit(Runnable task)
Future<?> submit(Callable<?> task)
返回的是接口其实是说返回的是这个接口的实现类ThreadPoolExecutor,让这个类去实现run
ExecutorService es = Executors.newFixedThreadPool(2);
es.submit(new Runnable());

es.shutdown();
//关闭线程池

Callable

RunnableProblems with the interface:

  1. The run method is void
  2. exception cannot be ruled out
  3. It can be solved with the Callable<V> interface, which is used in the same way as Runnable
  4. Override Callable's call()
ExecutorService es = Executors.newFixedThreadPool(2);
es.submit(new Callable<?>());

//拿到返回值的方式Futere接口
Future<T> f = es.submit(new Callable<T>());
es.shutdown();
//关闭线程池

Multi-threaded asynchronous computing example

ExecutorService es = Executors.newFixedThreadPool(2);
es.submit(task);
es.submit(task);
//可以往构造器里面传入参数

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325943816&siteId=291194637