50 Analysis of Java threads interview questions and answers

The following are popular Java thread-related interview questions, you can use it to get ready for an interview.

1) What is the thread?
A thread is the smallest unit of an operating system capable of operation schedule, which is included in the process, the actual operation of the unit process. Programmers can use it for multiprocessor programming, you can use multiple threads to accelerate compute-intensive tasks. For example, if a thread to complete a task to be 100 ms, then just 10 milliseconds to complete a task with ten threads. In the Java language level provides excellent support for multiple threads, it is also a good selling point.

2) threads and processes What is the difference?
A thread is a subset of the process, a process can have a lot of threads, each thread in parallel to perform different tasks. Different processes using different memory space, and all threads share a same memory space. Do not confuse it and stack memory, each thread has a separate stack memory used to store local data.

3) How to achieve a thread in Java?
3.1 inherit the Thread class to create a thread
3.2 implements the Runnable interface to create threads
3.3 implement Callable interface to create Thread by thread wrapper FutureTask
3.4 ExecutorService, Callable, Future achieve that return results thread

4) with a Runnable or Thread?
The problem is the question of follow-up, we all know that we can inherit the Thread class or Runnable interface calls to implement threads, the problem is, that method is better? Under what circumstances to use it? This question is easy to answer if you know Java does not support multiple inheritance classes, but allows you to call multiple interfaces. So if you want to inherit from other classes, of course, is to call the Runnable interface better.

Runnable easier to share resources, a resource can handle multiple threads simultaneously.

5) Thread class start () and run () method What is the difference?
This question is often asked, but still able to distinguish from the interviewer understanding of the Java threading model. start () method is used to start a new thread is created, and the start () internally calls the run () method, and this effect directly call run () method is not the same. When you call the run () method will only be called for execution in the original thread, no new thread is started, start () method will start a new thread.

6) Java in Runnable and Callable What is the difference?
Runnable and Callable on behalf of those tasks to be performed in different threads. From the beginning there JDK1.0 Runnable, Callable in JDK1.5 increased. The main difference is the Callable call () method returns a value and throw an exception, but the Runnable run () method do not have these features. Callable Future object may return loaded with the calculation result.

7) How to force starts a thread?
This problem is like how to force Java garbage collection, there is no such a method to carry out garbage collection, although you can use the System.gc (), but does not guarantee success. In Java there is no way forced to start a thread, it is the thread scheduler controls and Java did not disclose the relevant API.

8) Java in CyclicBarrier and CountDownLatch What is the difference?
CyclicBarrier and CountDownLatch can be used to make a set of threads waiting for the other thread. The difference is that with CyclicBarrier, CountdownLatch can not be reused.

What 9) Java memory model?

10) What are the volatile variables in Java?
volatile is a special modifier, only the member variables in order to use it. In the case of Java concurrent programs Missing Sync class, multi-threaded operating member variables are transparent to other threads. Under volatile variable can guarantee a read operation will take place after the previous write operation is to rule on a question of volatile variables. Click here to view the contents of more volatile.

https://www.cnblogs.com/linjiqin/p/3212737.html

11) What is thread safe? Vector is a thread-safe class do?
If your code where the process has multiple threads running at the same time, and these threads may run this code at the same time. If the result of each run single-threaded operating results and the same, and also the values of other variables and expectations are the same, that is thread-safe. A thread-safe counter the same instance of an object class will not miscalculations in the case of the use of multiple threads. Obviously you can collection class into two groups, thread-safe and non-thread-safe. Vector is synchronized methods to achieve thread-safe, but it and similar ArrayList is not thread safe.

12) Java What is a race condition? An example will be described.
Race conditions can cause some bugs in the program of concurrency. Multithreading some resources will have competition when race conditions, if the program fails to execute first competition discharged later executed, then the whole program there will be bugs some uncertain. This is difficult to find bugs and will be repeated, because the random competition between threads.

13) How to stop a thread in Java?
Java API provides a very rich but did not provide an API to stop the thread. JDK 1.0 would have been something like stop (), suspend () and resume () method of control, but because of the potential threat and therefore a deadlock in subsequent versions of the JDK they have been abandoned, after the designer of the Java API would not provide a compatible and thread-safe way to stop a thread. When the run () or call () method of executing the thread will automatically end, if you want to end a thread manually, you can use volatile boolean variable to exit the run () method of recycling or cancel the task to interrupt thread.

14) What a thread exception will occur when running?
This is a very tricky Java interview questions in a job interview I encountered, simply put, if the exception is not caught the thread will stop executing. Thread.UncaughtExceptionHandler for handling uncaught exceptions caused by a sudden interruption of a thread-line interface. When an uncaught exception will cause disruption JVM thread will use Thread.getUncaughtExceptionHandler () to query the thread and the thread UncaughtExceptionHandler and abnormal passed as parameters to the uncaughtException handler () method for processing.

15) 如何在两个线程间共享数据?
实现Runnable接口创建线程,如:
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}

16) Java中notify 和 notifyAll有什么区别?
这又是一个刁钻的问题,因为多线程可以等待单监控锁,Java API 的设计人员提供了一些方法当等待条件改变的时候通知它们,但是这些方法没有完全实现。notify()方法不能唤醒某个具体的线程,所以只有一个线程在等待的时候它才有用武之地。而notifyAll()唤醒所有线程并允许他们争夺锁确保了至少有一个线程能继续运行。

18) 什么是ThreadLocal变量?
线程范围内的共享变量,每个线程只能访问他自己的,不能访问别的线程。

19) 什么是FutureTask?
在Java并发程序中FutureTask表示一个可以取消的异步运算。它有启动和取消运算、查询运算是否完成和取回运算结果等方法。只有当运算完成的时候结果才能取回,如果运算尚未完成get方法将会阻塞。一个FutureTask对象可以对调用了Callable和Runnable的对象进行包装,由于FutureTask也是调用了Runnable接口所以它可以提交给Executor来执行。

20) Java中interrupted 和 isInterruptedd方法的区别?
interrupted() 和 isInterrupted()的主要区别是前者会将中断状态清除而后者不会。Java多线程的中断机制是用内部标识来实现的,调用Thread.interrupt()来中断一个线程就会设置中断标识为true。当中断线程调用静态方法Thread.interrupted()来检查中断状态时,中断状态会被清零。而非静态方法isInterrupted()用来查询其它线程的中断状态且不会改变中断状态标识。简单的说就是任何抛出InterruptedException异常的方法都会将中断状态清零。无论如何,一个线程的中断状态有有可能被其它线程调用中断来改变。

21) 为什么wait和notify方法要在同步块中调用?
主要是因为Java API强制要求这样做,如果你不这么做,你的代码会抛出IllegalMonitorStateException异常。还有一个原因是为了避免wait和notify之间产生竞态条件。

23) Java中的同步集合与并发集合有什么区别?

同步集合与并发集合都为多线程和并发提供了合适的线程安全的集合,不过并发集合的可扩展性更高。在Java1.5之前程序员们只有同步集合来用且在多线程并发的时候会导致争用,阻碍了系统的扩展性。Java5介绍了并发集合像HashMap,不仅提供线程安全还用锁分离和内部分区等现代技术提高了可扩展性。

24) Java中堆和栈有什么不同?
为什么把这个问题归类在多线程和并发面试题里?因为栈是一块和线程紧密相关的内存区域。每个线程都有自己的栈内存,用于存储本地变量,方法参数和栈调用,一个线程中存储的变量对其它线程是不可见的。而堆是所有线程共享的一片公用内存区域。对象都在堆里创建,为了提升效率线程会从堆中弄一个缓存到自己的栈,如果多个线程使用该变量就可能引发问题,这时volatile 变量就可以发挥作用了,它要求线程从主存中读取变量的值。

(1)存放内容不同:
栈内存:用来存放基本数据类型变量和引用类型变量。
堆内存:用来存放运行时通过new关键字创建的对象。

(2)生命周期不同:
栈的生命周期与线程相同,随线程而生,随线程而亡,是线程私有的。
堆的生命周期与JVM相同,JVM启动时创建,JVM停止时销毁,是线程共享的。

25)什么是线程池? 为什么要使用它?
创建线程要花费昂贵的资源和时间,如果任务来了才创建线程那么响应时间会变长,而且一个进程能创建的线程数有限。为了避免这些问题,在程序启动的时候就创建若干线程来响应处理,它们被称为线程池,里面的线程叫工作线程。

缓存线程池(可变尺寸的线程池)、固定大小的线程池、调度线程池、单例线程池、自定义线程池

26)如何写代码来解决生产者消费者问题?
在现实中你解决的许多线程问题都属于生产者消费者模型,就是一个线程生产任务供其它线程进行消费,你必须知道怎么进行线程间通信来解决这个问题。比较低级的办法是用wait和notify来解决这个问题,比较赞的办法是用Semaphore 或者 BlockingQueue来实现生产者消费者模型,这篇教程有实现它。

用wait和notify实现
用Semaphore信号量实现
BlockingQueue/ConcurrentLinkedQueue

29) 怎么检测一个线程是否拥有锁?
我一直不知道我们竟然可以检测一个线程是否拥有锁,直到我参加了一次电话面试。在java.lang.Thread中有一个方法叫holdsLock(),它返回true如果当且仅当当前线程拥有某个具体对象的锁。

30) 你如何在Java中获取线程堆栈?
对于不同的操作系统,有多种方法来获得Java进程的线程堆栈。当你获取线程堆栈时,JVM会把所有线程的状态存到日志文件或者输出到控制台。在Windows你可以使用Ctrl + Break组合键来获取线程堆栈,Linux下用kill -3命令。你也可以用jstack这个工具来获取,它对线程id进行操作,你可以用jps这个工具找到id。

31) JVM中哪个参数是用来控制线程的栈堆栈小的
这个问题很简单, -Xss参数用来控制线程的堆栈大小。你可以查看JVM配置列表来了解这个参数的更多信息。

32) Java中synchronized 和 ReentrantLock 有什么不同?
Java在过去很长一段时间只能通过synchronized关键字来实现互斥,它有一些缺点。比如你不能扩展锁之外的方法或者块边界,尝试获取锁时不能中途取消等。
Java 5 通过Lock接口提供了更复杂的控制来解决这些问题。 ReentrantLock 类实现了 Lock,它拥有与 synchronized 相同的并发性和内存语义且它还具有可扩展性。

33) 有三个线程T1,T2,T3,怎么确保它们按顺序执行?
在多线程中有多种方法让线程按特定顺序执行,你可以用线程类的join()方法在一个线程中启动另一个线程,另外一个线程完成该线程继续执行。为了确保三个线程的顺序你应该先启动最后一个(T3调用T2,T2调用T1),这样T1就会先完成而T3最后完成。

35) Java中ConcurrentHashMap的并发度是什么?
ConcurrentHashMap把实际map划分成若干部分来实现它的可扩展性和线程安全。这种划分是使用并发度获得的,它是ConcurrentHashMap类构造函数的一个可选参数,默认值为16,这样在多线程情况下就能避免争用。

36) Java中Semaphore是什么?
Java中的Semaphore是一种新的同步类,它是一个计数信号。从概念上讲,从概念上讲,信号量维护了一个许可集合。如有必要,在许可可用前会阻塞每一个 acquire(),然后再获取该许可。每个 release()添加一个许可,从而可能释放一个正在阻塞的获取者。但是,不使用实际的许可对象,Semaphore只对可用许可的号码进行计数,并采取相应的行动。


37)如果你提交任务时,线程池队列已满。会时发会生什么?

这个问题问得很狡猾,许多程序员会认为该任务会阻塞直到线程池队列有空位。事实上如果一个任务不能被调度执行那么ThreadPoolExecutor’s submit()方法将会抛出一个RejectedExecutionException异常。

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

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

39) 什么是阻塞式方法?

阻塞式方法是指程序会一直等待该方法完成期间不做其他事情,ServerSocket的accept()方法就是一直等待客户端连接。这里的阻塞是指调用结果返回之前,当前线程会被挂起,直到得到结果之后才会返回。此外,还有异步和非阻塞式方法在任务完成前就返回。

44) Java中的ReadWriteLock是什么?
一般而言,读写锁是用来提升并发程序性能的锁分离技术的成果。Java中的ReadWriteLock是Java 5 中新增的一个接口,一个ReadWriteLock维护一对关联的锁,一个用于只读操作一个用于写。在没有写线程的情况下一个读锁可能会同时被多个读线程持有。写锁是独占的,你可以使用JDK中的ReentrantReadWriteLock来实现这个规则,它最多支持65535个写锁和65535个读锁。

46)volatile 变量和 atomic 变量有什么不同?
这是个有趣的问题。首先,volatile 变量和 atomic 变量看起来很像,但功能却不一样。Volatile变量可以确保先行关系,即写操作会发生在后续的读操作之前, 但它并不能保证原子性。例如用volatile修饰count变量那么 count++ 操作就不是原子性的。而AtomicInteger类提供的atomic方法可以让这种操作具有原子性如getAndIncrement()方法会原子性的进行增量操作把当前值加一,其它数据类型和引用变量也可以进行相似操作。

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

这个问题坑了很多Java程序员,若你能想到锁是否释放这条线索来回答还有点希望答对。无论你的同步块是正常还是异常退出的,里面的线程都会释放锁,所以对比锁接口我更喜欢同步块,因为它不用我花费精力去释放锁,该功能可以在finally block里释放锁实现。

Guess you like

Origin www.cnblogs.com/linjiqin/p/11315694.html