Lang:简体中文

java面试线程问题

日期:2025-09-08 / 来源:面试宝典

深入剖析线程面试难题

在java面试中,线程相关的问题是考察的重点,它能反映出面试者对java多线程编程的理解和掌握程度。下面就来详细介绍一些常见的java线程面试问题。

线程的创建方式

java中创建线程主要有三种方式。第一种是继承thread类,重写run方法。示例代码如下:

java

public class mythread extends thread {

@override

public void run() {

system.out.println("this is a thread created by extending thread class.");

}

public static void main(string[] args) {

mythread thread = new mythread();

thread.start();

}

}

第二种是实现runnable接口,实现run方法。示例如下:

java

public class myrunnable implements runnable {

@override

public void run() {

system.out.println("this is a thread created by implementing runnable interface.");

}

public static void main(string[] args) {

thread thread = new thread(new myrunnable());

thread.start();

}

}

第三种是使用callable和futuretask,这种方式可以有返回值。示例代码:

java

import java.util.concurrent.callable;

import java.util.concurrent.futuretask;

public class mycallable implements callable {

@override

public integer call() throws exception {

return 123;

}

public static void main(string[] args) {

mycallable callable = new mycallable();

futuretask futuretask = new futuretask<>(callable);

thread thread = new thread(futuretask);

thread.start();

try {

system.out.println(futuretask.get());

} catch (exception e) {

e.printstacktrace();

}

}

}

线程的生命周期

线程的生命周期包括新建、就绪、运行、阻塞和死亡五种状态。新建状态是指线程对象被创建,如new thread()。就绪状态是线程调用start方法后,等待cpu调度。运行状态是线程获得cpu时间片开始执行。阻塞状态是线程因为某些原因暂停执行,如等待i/o操作。死亡状态是线程执行完毕或者出现异常终止。

线程同步问题

在多线程环境下,为了保证数据的一致性和完整性,需要进行线程同步。常见的同步方式有synchronized关键字和lock接口。使用synchronized关键字可以修饰方法或者代码块。示例:

java

public class synchronizedexample {

private int count = 0;

public synchronized void increment() {

count++;

}

public static void main(string[] args) throws interruptedexception {

synchronizedexample example = new synchronizedexample();

thread t1 = new thread(() -> {

for (int i = 0; i < 1000; i++) {

example.increment();

}

});

thread t2 = new thread(() -> {

for (int i = 0; i < 1000; i++) {

example.increment();

}

});

t1.start();

t2.start();

t1.join();

t2.join();

system.out.println(example.count);

}

}

使用lock接口可以更灵活地控制锁的获取和释放。示例:

java

import java.util.concurrent.locks.lock;

import java.util.concurrent.locks.reentrantlock;

public class lockexample {

private int count = 0;

private lock lock = new reentrantlock();

public void increment() {

lock.lock();

try {

count++;

} finally {

lock.unlock();

}

}

public static void main(string[] args) throws interruptedexception {

lockexample example = new lockexample();

thread t1 = new thread(() -> {

for (int i = 0; i < 1000; i++) {

example.increment();

}

});

thread t2 = new thread(() -> {

for (int i = 0; i < 1000; i++) {

example.increment();

}

});

t1.start();

t2.start();

t1.join();

t2.join();

system.out.println(example.count);

}

}

线程池的使用

线程池可以复用线程,减少线程创建和销毁的开销。java中提供了executorservice接口和threadpoolexecutor类来实现线程池。示例:

java

import java.util.concurrent.executorservice;

import java.util.concurrent.executors;

public class threadpoolexample {

public static void main(string[] args) {

executorservice executorservice = executors.newfixedthreadpool(5);

for (int i = 0; i < 10; i++) {

executorservice.execute(() -> {

system.out.println(thread.currentthread().getname());

});

}

executorservice.shutdown();

}

}

死锁问题

死锁是指两个或多个线程在执行过程中,因争夺资源而造成的一种互相等待的现象。产生死锁需要满足四个条件:互斥条件、请求和保持条件、不剥夺条件和循环等待条件。避免死锁可以通过破坏这四个条件中的一个来实现。例如,按顺序获取锁可以避免循环等待条件。示例代码:

java

public class deadlockexample {

private static final object lock1 = new object();

private static final object lock2 = new object();

public static void main(string[] args) {

thread t1 = new thread(() -> {

synchronized (lock1) {

system.out.println("thread 1 acquired lock1");

try {

thread.sleep(100);

} catch (interruptedexception e) {

e.printstacktrace();

}

synchronized (lock2) {

system.out.println("thread 1 acquired lock2");

}

}

});

thread t2 = new thread(() -> {

synchronized (lock2) {

system.out.println("thread 2 acquired lock2");

try {

thread.sleep(100);

} catch (interruptedexception e) {

e.printstacktrace();

}

synchronized (lock1) {

system.out.println("thread 2 acquired lock1");

}

}

});

t1.start();

t2.start();

}

}

以下为推荐内容

微信二维码