Please explain how to implement a multi-threaded application!
There is a class of type CRUD with static methods and a collection with elements:

public class Crud { protected static List<User> users = Collections.synchronizedList(new ArrayList<>()); static { initialize(); } protected static void initialize() { users.add(new User("Name1", "Surname1", 27)); users.add(new User("Name2", "Surname2", 27)); users.add(new User("Name3", "Surname3", 26)); users.add(new User("Name4", "Surname4", 26)); users.add(new User("Name5", "Surname5", 28)); } public static void insert(User user) { users.add(user); } public static void delete(int index) { users.remove(index); } public static void update(int index, User user) { users.set(index, user); } public static void show() { Iterator it = users.iterator(); while (it.hasNext()) { User user = (User)it.next(); System.out.println(user.toString()); } } public static void show(int index) { User user = users.get(index); System.out.println(user.toString()); } } 

You need to implement multi-threading in the main(String[] args) method to simultaneously extract and modify data in the collection.
This is a purely test application, not an implementation, purely for educational purposes.
Thank you in advance for your help.

  • After you implement multithreading, you will encounter the fact that your class is not flow safe. In particular, iteration of the synchronized list is not a thread-safe operation and requires synchronization (between the next() calls, another thread can easily intervene by adding or deleting an element). Therefore, it will be possible to refuse the wrapper and synchronize any access to the collection (except for the designer) by hand. - andreycha

1 answer 1

I will not describe the classic approach with manual creation of individual threads (although you should familiarize yourself with it in order to know how it works inside), the most commonly used approach for parallelizing tasks is creating and using ThreadPoolExecutor (like implementing Executor and ExecutorService interfaces):

 // создаст ThreadPoolExecutor из четырех тредов ExecutorService executor = Executors.newFixedThreadPool(4); executor.submit(new Runnable() { // этот код будет выполняться в произвольном потоке ThreadPoolExecutor crud.show(1); }); // запрещает добавление новых задач и подготавливает ThreadPoolExecutor к завершению executor.shutdown(); // ожидает завершение ThreadPoolExecutor'a executor.awaitTermination(1, TimeUnit.SECONDS); 
  • Thank you, but can you write down the classic approach?)))) I understand that this time is expensive. - El Salvadore
  • @ElSalvadore only if the time is - etki
  • OK. and also, is synchronization exactly implemented in this example? - El Salvadore
  • @ElSalvadore is not, it definitely will not be here. I perceived the question as the need to create an environment in which multi-thread access will be organized and only, the task of preparing for such conditions lies with the executable code, i.e. your crud. At the moment, if you distribute a lot of state-changing tasks in TPE, you will most likely observe relevant phenomena like lost records. - etki