Threads explained

           Before we take a look at to understand threads parallel and concurrent!

Concurrency (Concurrent): at the same time interval turn to do multiple events, multithreading is a form of concurrent.

Parallel (Parallel): do multiple events at the same time.

 

Concurrent: a processor on a "simultaneous" a plurality of processing tasks; parallel: processing a plurality of tasks simultaneously on multiple processors.

Therefore, concurrent programming goal is to fully utilize every core processors to achieve the highest processing performance.

There is a clear analogy:
Concurrency: a person can eat three apples.

Parallel: three people eating three apples.

 

The difference between threads and processes?

During execution process has a separate memory unit, and multiple threads share memory resources, reduce the switching frequency and thus more efficient;

A thread is a physical process, is the basic unit of cpu scheduling and dispatch, is smaller than the program of the basic unit can operate independently;

The process is running and the basic unit of resource allocation, a program has at least one process, a process has at least one thread;

You can execute concurrently across multiple threads in the same process;

 

 What are the different ways to create a thread?

①. Inheritance Thread class Thread class is created

 

  • Subclass definition of the Thread class and override the run method of the class, method body that represents the run method of the thread to complete the task. Thus the run () method is called executable.
  • Create an instance of a subclass of Thread that created thread object.
  • Call the thread object's start () method to start the thread.

②. Create threads through the Runnable interface class

  • Runnable defined interface implementation class, interface and override the run () method, the method body run () method of the same thread of execution threads.
  • Create an instance of Runnable implementation class, and so as an example of a target to create Thread Thread object, the Thread object is the real thread object.
  • Call the thread object's start () method to start the thread.

 

③. Create a thread through Callable and Future

 

  • Create Callable interface implementation class, and implements call () method, the call () method as a thread of execution, and return values.
  • Callable create an instance of the implementation class, using packaging Callable FutureTask class object, which object encapsulates FutureTask Callable object of the call () method returns the value.
  • Use FutureTask Thread object as a target object to create and start a new thread.
  • With FutureTask object get () method to obtain the return value after the end of the sub-thread execution.

The difference between the runnable and callable

 

  • The return value run Runnable interface () method is void, just do it purely to perform the run () method code in it;
  • call Callable interface () method returns a value, is a generic, and Future, FutureTask can be used to obtain the results asynchronously with the execution.

 

 

Thread What state?

Thread usually have five states, create, ready, running, blocking and death.

 

  • Creating state. In the generation thread object, start method of the object and does not call, this is the state of the thread is created.
  • Ready state. When the call start method of thread object, the thread into the ready state,
  •                   But this time the thread scheduler does not take into the threads to the current thread, this time in a ready state.
  •                   After thread running from the back after waiting or sleeping, also in a ready state.
  • Operating status. Thread sets a thread scheduler in a ready state for the current thread, then thread enters the run state, the run starts running among the function code.
  • Blocking state. When running thread, it is suspended, usually for waiting to happen at a certain time and then continue to run after (for example, a resource ready). sleep, suspend, wait thread and other methods are available to cause obstruction.
  • Death state. If a thread run method of execution end or stop method is called, the thread will die. For thread dead, you can no longer use the start method to make it into the ready

 sleep () and wait () What is the difference

  • sleep():方法是线程类(Thread)的静态方法,让调用线程进入睡眠状态,让出执行机会给其他线程,等到休眠时间结束后,线程进入就绪状态和其他线程一起竞争cpu的执行时间。因为sleep() 是static静态的方法,他不能改变对象的机锁,当一个synchronized块中调用了sleep() 方法,线程虽然进入休眠,但是对象的机锁没有被释放,其他线程依然无法访问这个对象。
  • wait():wait()是Object类的方法,当一个线程执行到wait方法时,它就进入到一个和该对象相关的等待池,同时释放对象的机锁,使得其他线程能够访问,可以通过notify,notifyAll方法来唤醒等待的线程

notify()和 notifyAll()有什么区别?

notify()方法只随机唤醒一个 wait 线程

 notifyAll()方法唤醒所有 wait 线程

 

线程的 run()和 start()有什么区别

 start()方法来启动一个线程,真正实现了多线程运行。

run(),其实就相当于是调用了一个普通函数而已

 

 创建线程池有哪几种方式?

①. newFixedThreadPool(int nThreads)

创建一个固定长度的线程池,每当提交一个任务就创建一个线程,直到达到线程池的最大数量,这时线程规模将不再变化,当线程发生未预期的错误而结束时,线程池会补充一个新的线程。

②. newCachedThreadPool()

创建一个可缓存的线程池,如果线程池的规模超过了处理需求,将自动回收空闲线程,而当需求增加时,则可以自动添加新线程,线程池的规模不存在任何限制。

③. newSingleThreadExecutor()

这是一个单线程的Executor,它创建单个工作线程来执行任务,如果这个线程异常结束,会创建一个新的来替代它;它的特点是能确保依照任务在队列中的顺序来串行执行。

④. newScheduledThreadPool(int corePoolSize)

创建了一个固定长度的线程池,而且以延迟或定时的方式来执行任务,类似于Timer。

 

线程池中 submit()和 execute()方法有什么区别?

  • 接收的参数不一样
  • submit有返回值,而execute没有
  • submit方便Exception处理

  在 java 程序中怎么保证多线程的运行安全?

线程安全在三个方面体现:

  • 原子性:提供互斥访问,同一时刻只能有一个线程对数据进行操作,(atomic,synchronized);
  • 可见性:一个线程对主内存的修改可以及时地被其他线程看到,(synchronized,volatile);
  • 有序性:一个线程观察其他线程中的指令执行顺序,由于指令重排序,该观察结果一般杂乱无序,(happens-before原则)。

 

Guess you like

Origin www.cnblogs.com/zzjlxy-225223/p/11291563.html