多线程并发场景题(持续更新)

多线程问题

1.两线程交替打印a,b,共打印 100 次:

分析:

  1. 需要先打印出 a
  2. 需要保证交替打印
  3. 需要保证打印 100 次,不多不少

思路:使用一个 flag 变量进行通信, 当 flag 为奇数时打印 a,为偶数时打印 b。线程 t1 负责打印 a,flag 为奇数时它会await,并且使用 while 包裹防止虚假唤醒,当 flag 不是奇数时会 await,等待另一个线程打印完成后唤醒它。对 t2 线程也是一样。

注意:

  1. lock.unlock()需要写在 finally 里面保证一定释放
  2. await 一定在锁范围内,否则报illegalMoniterStateException,await 语义是放弃对锁的占有。
  3. 对打印条件while 判断,对打印次数while判断,但是在打印的时候需要再判断一次,双端判断
//	初版ReentrantLock 实现版本
public class AlternatePrint{
    
    public static int cnt = 1;
    static ReentrantLock lock = new ReentrantLock();

    public static void main(){

        Condition condition = lock.newCondition();

        new Thread(()->{

            while(cnt<=100){

                try{
                    lock.lock();
                    while(cnt%2==1){
                        condition.await();
                    }
                    if(cnt<=100)System.out.println("a: "+Thread.currentThread().getName()+" -- "+cnt++);
                    condition.signal();
                } catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
        },"t1").start();


        new Thread(()->{

            while(cnt<=100){

                try{
                    lock.lock();
                    while(cnt%2==0){
                        condition.await();
                    }
                    if(cnt<=100)System.out.println("b: "+Thread.currentThread().getName()+" -- "+cnt++);
                    condition.signal();
                } catch(InterruptedException e){
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
        },"t2").start();

    }
}

存在问题:两个线程间还是有很多代码是重复的,我需要把重复代码抽象一下;并且再尝试使用 semaphore 和 lockSupport 去实现

//		优化版本condition
public class AlternatePrint{
  
  public static void main(){
    
    	PrintChar pc = new PrintChar();
    	new Thread(()->{
        pc.doPrint('a',1);
      },"t1").start();
    
    	new Thread(()->{
        pc.doPrint('b',0);
      },"t2").start();
  }
  
  static class PrintChar{
    
      public int cnt = 1;
      public ReentrantLock lock = new ReentrantLock();
    	public Condition condition = lock.newCondition();
    
    	public void doPrint(char c,int flag){
        
        while(cnt<=100){
     			lock.lock();
          try{
            while(cnt%2==flag){condition.await();}
            if(cnt<=100)System.out.println(c+" : "+Thread.currentThread().getName()+" -- "+cnt++);
            condition.signal();
          }catch(InterruptedException e){
            	e.printStackTrace();
          }finally{
            lock.unlock();
          }
        }//	while cnt<=100	
     } 
  }
}
//	lockSupport版本

2.手写线程池

梳理流程:

  1. 创建线程池类
  2. 创建成员变量和含参构造
  3. 继承线程类实现线程内部类
  4. 添加线程池 execute 方法
public class MyThreadPool {

    private int corePool;
    private BlockingQueue<Runnable> taskQueue;
    private WorkThread[] threads;

    public MyThreadPool(int corePool){
        this.corePool = corePool;
        taskQueue = new LinkedBlockingQueue<>();
        threads = new WorkThread[corePool];

        for(int i=1;i<=corePool;i++){
            threads[i] = new WorkThread("thread--"+i);
            threads[i].start();
        }
    }

    class WorkThread extends Thread{

        public WorkThread(String name){
            super(name);
        }

        @Override
        public void run(){

            Runnable task;
            while (true){
                synchronized(taskQueue){
                    while (taskQueue.isEmpty()){
                        try {
                            taskQueue.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    task = taskQueue.poll();
                }
                if(task!=null)task.run();
            }
        }
    }

    public void execute(Runnable task){
        synchronized(taskQueue){
            taskQueue.add(task);
            taskQueue.notify();
        }
    }
}

3.写锁降级

ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

  //  获取写锁
  lock.writeLock().lock();

  //  持有写锁尝试加读锁(可重入锁)
  lock.readLock().lock();

  //  释放写锁
  lock.writeLock().unlock();

  //  释放读锁
  lock.readLock().unlock();

4.解决写线程饥饿问题

发展过程:读、写互斥 -> 读读共享,读写互斥 -> 乐观锁

static StampedLock stampedLock = new StampedLock();

    public void read(){

        long stamp = stampedLock.tryOptimisticRead();

        System.out.println("reading");

        if(!stampedLock.validate(stamp)){
          
            System.out.println("被修改了,换用 read 锁重新读");

            stamp = stampedLock.readLock();

            System.out.println("redo reading!");

            stampedLock.unlock(stamp);
        }
    }

5.CompletableFuture使用

基本不使用构造,使用两个静态方法完成

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1, 2,
                3, TimeUnit.SECONDS, new LinkedBlockingQueue<>(),
                Executors.defaultThreadFactory(),new ThreadPoolExecutor.DiscardPolicy());

 CompletableFuture.supplyAsync(()->{
     return 1024;
 },threadPoolExecutor).whenComplete((a,e)->{
     System.out.println(a);
 });

6.实现线程中断的三大方法

  1. volatile 实现:
    private volatile boolean isStop;
    
    @Test
    public void volatileImpl() {
        new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + " is running!");
                if (isStop) {
                    System.out.println(Thread.currentThread().getName() + " interrupt!");
                    break;
                }
            }
        }, "t1").start();
    
        try {Thread.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
    
        new Thread(() -> {
            isStop = true;
        }, "t2").start();
    }
    
  2. Atomic 类实现:
    private AtomicBoolean atomicBoolean = new AtomicBoolean(false);
    
    @Test
    public void atomicImpl() {
        new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + " is running!");
                if (atomicBoolean.get()) {
                    System.out.println(Thread.currentThread().getName() + " interrupt!");
                    break;
                }
            }
        }, "t1").start();
    
        try {Thread.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
    
        new Thread(() -> {
            atomicBoolean.set(true);
        }, "t2").start();
    }
    
    
  3. interrupt实现:
    public void interruptImpl() {
        Thread t1 = new Thread(() -> {
            while (true) {
                System.out.println(Thread.currentThread().getName() + " is running!");
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println(Thread.currentThread().getName() + " interrupt!");
                    break;
                }
            }
        }, "t1");
        t1.start();
    
      	try {Thread.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}
    
        new Thread(() -> {
            t1.interrupt();
        }, "t2").start();
    }
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Kafka提供的Java客户端API来实现多线程消费Kafka消息。具体步骤如下: 1. 创建Kafka消费者实例,设置消费者配置参数,如bootstrap.servers、group.id等。 2. 创建多个消费线程,每个线程都创建一个Kafka消费者实例并订阅相同的Kafka主。 3. 在每个消费线程中,使用Kafka消费者实例的poll()方法从Kafka主中拉取消息。 4. 处理拉取到的消息,可以将消息放入线程安全的队列中,由其他线程进行处理。 5. 在消费线程中,使用Kafka消费者实例的commitSync()方法提交消费偏移量,确保消息被成功处理。 6. 在主线程中,等待所有消费线程完成消费任务后,关闭Kafka消费者实例。 示例代码如下: ```java public class KafkaConsumerDemo { private static final String TOPIC_NAME = "test-topic"; private static final int NUM_THREADS = 3; public static void main(String[] args) { Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "false"); props.put("auto.offset.reset", "earliest"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS); for (int i = 0; i < NUM_THREADS; i++) { executorService.execute(() -> { KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singleton(TOPIC_NAME)); while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { // 处理消息 System.out.printf("Thread: %s, offset: %d, key: %s, value: %s\n", Thread.currentThread().getName(), record.offset(), record.key(), record.value()); } consumer.commitSync(); } }); } Runtime.getRuntime().addShutdownHook(new Thread(() -> { executorService.shutdownNow(); })); } } ``` 上面的示例代码创建了3个消费线程,每个线程都创建了一个Kafka消费者实例,并订阅了名为test-topic的Kafka主。在每个消费线程中,使用poll()方法从Kafka主中拉取消息,并处理消息。在消费线程中,使用commitSync()方法提交消费偏移量。在主线程中,通过addShutdownHook()方法注册了一个钩子函数,用于在程序退出时关闭线程池。 ### 回答2: Java多线程消费Kafka是一种常见的消息消费方式,适用于需要高性能和高并发处理消息的场景。下面是一个简单的示例代码,用于演示Java多线程消费Kafka的基本思路。 1. 首先,我们需要引入Kafka的Java客户端依赖包,例如Apache Kafka提供的`kafka-clients`库。 2. 然后,我们创建一个消费者线程类`ConsumerThread`,实现`Runnable`接口,该类的主要功能是从Kafka主订阅消息并进行处理。 3. 在`ConsumerThread`类的构造函数中,我们可以传入一些配置参数,例如Kafka的服务器地址、消费者组ID以及要消费的主等。 4. 在`run()`方法中,我们可以实例化一个Kafka消费者对象,并设置消费者的配置参数。然后,通过`consumer.subscribe()`方法订阅指定的主。 5. 在一个无限循环中,通过`consumer.poll()`方法从Kafka中拉取消息。拉取到的消息会被封装成一个`ConsumerRecords`对象。 6. 迭代`ConsumerRecords`对象,并逐条处理每条消息。处理的逻辑可以根据业务需求自定义,例如将消息保存到数据库、进行计算或者发送到其他系统等。 7. 最后,记得在合适的时候关闭消费者对象,释放资源。 使用多线程消费Kafka能够提高消息消费的效率和并发性。可以考虑将消费者线程实例化为一个线程池,并指定线程数,以实现并发处理。 需要注意的是,在多线程消费Kafka的场景下,可能会出现消息顺序不一致的情况。因此,需要根据业务需求来判断是否需要保持消息的顺序性。 ### 回答3: Java中可以使用Apache Kafka客户端库来实现多线程消费Kafka。多线程消费可以提高消费速度和并发性。 首先,我们需要创建一个Kafka消费者对象,指定要消费的主和Kafka集群的地址。然后,我们可以使用该消费者对象来订阅要消费的主。 接下来,我们可以创建多个线程来同时消费Kafka消息。每个线程都可以创建一个新的消费者实例,并在每个线程中运行一个消费循环,以接收并处理从Kafka中获取的消息。 在消费循环中,我们可以使用一个无限循环来持续地消费Kafka消息。在每次循环中,我们可以使用消费者对象的poll方法来获取一批新的消息。然后,我们可以遍历这批消息并进行相应的处理。 在处理消息的过程中,我们需要确保每个线程都能处理自己所接收到的消息,并且要考虑到线程安全性。可以使用锁或其他线程同步机制来保证多个线程之间的数据访问的一致性和互斥性。 此外,在处理消息的过程中,我们还可以对消息进行一些后续的处理,比如将消息保存到数据库、发送到其他系统或进行其他的业务逻辑操作。 最后,当需要停止消费线程时,我们可以调用消费者对象的close方法来关闭消费者。这将会释放消费者所占用的资源,并停止消费。 综上所述,通过创建多线程来消费Kafka消息可以提高消费速度和并发性,从而更好地满足高并发场景下的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值