java multithreading interview questions

java multithreading interview questions

 

 

How to understand multithreading?

 

1. Let the application do multiple things at a time to improve efficiency

2. Asynchronous effect can be achieved, the main thread returns success immediately, and the child thread continues to work

 

 

 

Program, process, thread relationship

 

A program has a process, and a process can have multiple threads, which are divided into main threads and sub-threads.

 

 

 

Understanding Concurrency and Parallelism

 

Concurrency: Through the CPU scheduling algorithm, users can appear to execute at the same time. In fact, it is switched at high speed through the CPU, not at the same time

Parallelism: Multiple CPU instances or multiple machines execute a piece of processing logic at the same time, which is true simultaneous;

 

 

 

How to create a thread?

 

1. Inherit Thread

2. Implement the Runnable interface (preferred)

 

Notice:

1. Rewrite the run() method

2. Call thread.start(), the thread is really started

 

 

 

various states of the thread

 

Runnable: When the start() method is called, a thread becomes executable, but it does not mean that it will start real execution immediately. Instead, it is put into a thread pool, and the thread scheduler decides when to suspend execution based on thread priority.

 

Running: The processor is already executing the thread's code. It will run until it is blocked, or give up the opportunity to execute by itself through the static method Thread.yield(). Considering the overhead caused by scene switching, the yield() method should not be called frequently.

 

Waiting (Waiting): The thread is in a blocked state due to waiting for the processing results of external processes such as I/O. Calling the currObject.wait( ) method will make the current thread enter the waiting state until other threads call currObject.notify() Or currObject.notifyAll() .

 

Sleeping: Overloaded methods Thread.sleep(milliseconds), Thread.sleep(milliseconds, nanoseconds) can force Java threads to sleep (suspend).

 

Blocked on I/O: When an I/O condition changes (such as reading a few bytes of data), it transitions to an executable state.

 

Blocked on synchronization: When the lock is acquired, it will enter the executing state.

 

 

 

How to use multithreading in general?

 

线程池,线程池工作原理

 

 

 

为什么要用线程池

 

1. 对于一些简单的工作,每次创建和销毁线程,比较耗资源

2. 每次创建和销毁线程,也让程序变慢

3. 每个进程的中,可以创建的线程是有限的,为了保护服务器,这也是必须的

 

 

 

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

 

两个方法都可以向线程池提交任务,execute()方法的返回类型是void,它定义在Executor接口中, 而submit()方法可以返回持有计算结果的Future对象,它定义在ExecutorService接口中,它扩展了Executor接口,其它线程池类像ThreadPoolExecutor和ScheduledThreadPoolExecutor都有这些方法。

 

 

 

什么是多线程的线程安全

 

保证共享资源在多线程的情况下不会出现错乱。

 

 

 

如何保证线程安全

 

synchronized

同步方法、同步类、同步块

缺点:不灵活

 

Lock

可重入锁

读写锁

1. 多个读同时读,不加锁

2. 多个写互斥

3. 有读有写,写优先,如果读写同时等待,那优先唤醒写

缺点:手动释放锁

 

同步集合、并发集合

同步集合:HashTable、Vector、Collections中的同步方法

并发集合:ConcurrentHashMap、CopyOnWriteArrayList、CopyOnWriteHashSet

 

Volatile

 

不变类

 

 

 

什么时候会出死锁

 

两个线程同时占有对方要的资源

如:

线程1有资源A,要资源B

线程2有资源B,要资源A

 

死锁发生的条件:

互斥条件:一个资源每次只能被一个进程使用。

请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。

不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。

循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。

 

解决方法:

有序资源分配法

银行家算法

 

 

 

如何控制线程

 

闭锁、栅栏、信号量

join控制线程有序进行

 

 

 

项目中用到哪些

 

信号量、线程池

 

 

 

如果同步块内的线程抛出异常会发生什么?

 

无论你的同步块是正常还是异常退出的,里面的线程都会释放锁;至于Lock就要在finally中释放锁

 

 

 

wait()和sleep()的区别

 

wait() 释放锁,sleep() 不释放锁

wait() 要notify() 或 notifyAll() 才有机会获得锁,sleep() 一段时间后,自动继续

 

 

http://www.importnew.com/12773.html

 

Guess you like

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