简体   繁体   中英

Concurrent ordered list in java

I'm currently working on a F1 simulator and I've stuggled to implement the scoreboard.

I've got multiple vehicles, each on one thread, that every 5s updates it's distance and speed. Also, I have another thread for the score, which again updates every 5s. My problem is having the score updated and sorted. Right now I'm using a ConcurrentHashMap that stores all the vehicles. Each vehicle updates and I just make it a list and sort it:

public void sortCars() {
    Collections.sort(sortedCars, Collections.reverseOrder());   
}

My main problem is as follow, the cars need to recieve the updated list somehow, I was making the list static but I was told by my proffesors that was bad as the list was constantly updating, so my other option is to give each vehicle a score and sort it each iteration in each thread.

I've tried using an exchanger but it's too slow. Thought of using a Callable inside a while but doesnt feel right. Do I have any other option appart from those listed? If not, which one is the best?

Forgot to add, that vehicles only modify themselves, and the score class only sorts it

Turns out best solution is to add a variable to Vehicle with the list, and each time i sort it in Score user a setter. Not exactly exactly what I wanted but works, I just wanted no variable but guess thats impossible.

Thinking about it, I've just been dumb.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM