Multi-threaded Java written test questions

  • topic

Simple implementation of online movie ticket purchase, please focus on the situation of multiple people buying a seat at the same time, you can use pseudo code

  • analysis

The first step to pass the question is to have a movie ticket pool to store it in List. Then start multiple threads to consume the tickets in the list.
There are two points to note in this question. The first is to ensure that the threads are started at the same time. We can use countDownLatch to achieve the
second one is to consume the list to ensure that tickets cannot be purchased repeatedly. Use CopyOnWriteList or Collections.synchronizedList(new ArrayList<>()) to achieve non-repetitive ticket purchases.

  • Code
package com.dairuijie.demo.study;

import java.util.concurrent.CountDownLatch;

public class MyThead extends Thread{
    
    
	
	private Integer num;
	
	private CountDownLatch countDownLatch;

	@Override
	public void run() {
    
    
		// TODO Auto-generated method stub
		try {
    
    
			countDownLatch.await();
		} catch (InterruptedException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		boolean removeFlag = BuyMovieTicket.list.remove(num);
		if(removeFlag) {
    
    
			System.err.println(Thread.currentThread().getName() + "抢到号码为:" + num);
			System.err.println(Thread.currentThread().getName()+"买票成功!");
		}else {
    
    
			System.err.println(Thread.currentThread().getName()+"买票失敗!");
		}
		super.run();
	}

	public MyThead(Integer num,CountDownLatch countDownLatch) throws InterruptedException {
    
    
		System.out.println(Thread.currentThread().getName() + "准备抢票的号码:" + num);
		this.num = num;
		this.countDownLatch = countDownLatch;
	}
	
	

}

package com.dairuijie.demo.study;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 
 * @模块名:Day01
 * @包名:com.dairuijie.demo.study
 * @描述:BuyMovieTicket.java
 * @版本:1.0
 * @创建人:drj
 * @创建时间:2020年3月28日下午9:46:06
 */
public class BuyMovieTicket {
    
    
	public  static List<Integer> list = Collections.synchronizedList(new ArrayList<>());//保證安全
	
	public static void main(String[] args) throws InterruptedException {
    
    
		for(int i=0; i< 5; i++) {
    
    
			 list.add(i);
		}
		CountDownLatch countDownLatch = new CountDownLatch(15);
		Random r = new Random();
		for(int i=0; i< 15; i++) {
    
    
			new MyThead(r.nextInt(5),countDownLatch).start();
			countDownLatch.countDown();
		}
		System.err.println(list);
	}
}

  • running result

main number ready to grab tickets: 3
main number ready to grab tickets: 0
main number ready to grab tickets: 1
main number ready to grab tickets: 0
main number ready to grab tickets: 0
main number ready to grab tickets: 2
main ready Number
to grab tickets: 0
main Number to grab tickets: 0 main Number to grab tickets: 4
main Number to grab tickets: 1
main Number to grab tickets: 2
main Number to grab tickets: 0
main Number: 1
main number to grab tickets: 2
main number to grab tickets: 2
[0, 1, 2, 3, 4]
Thread-8 grabbed numbers: 4
Thread-4 failed to buy tickets!
Thread-7 Ticket purchase failed!
Thread-6 grabbed the number: 0
Thread-6 successfully bought the ticket!
Thread-5 failed to buy the ticket!
Thread-1 failed to buy the ticket!
Thread-12 failed to buy the ticket!
Thread-0 grabbed the number: 3
Thread-0 successfully bought tickets!
Thread-13 failed to buy tickets!
Thread-8 successfully bought tickets!
Thread-3 failed to buy tickets!
Thread-2 failed to buy tickets!
Thread-11 failed to buy tickets!
Thread-10 failed to buy tickets!
Thread-9 grabbed the number: 1
Thread-9 successfully bought the ticket!
Thread-14 grabbed the number: 2
Thread-14 successfully bought the ticket!

Guess you like

Origin blog.csdn.net/qq_29897369/article/details/105188533