Skip to content

Instantly share code, notes, and snippets.

@1ou
Created June 23, 2021 07:43
Show Gist options
  • Save 1ou/37f85a5d0783caec9c7813f2108d003e to your computer and use it in GitHub Desktop.
Save 1ou/37f85a5d0783caec9c7813f2108d003e to your computer and use it in GitHub Desktop.
fff
example
class UserDto {
Int id;
String name;
}
Integer threadsNumber = 4;
public class Worker implements Runnable {
private List<UserDto> outputScraper;
private CountDownLatch countDownLatch;
public Worker(List<UserDto> users, CountDownLatch countDownLatch, ConcurrentHashMap<Integer, UserDto> result) {
this.users = users;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 0; i < users.size(); i++) {
List<UserDto> usersToPay = restTemplate.getUsersToPay(users.get(i));
for (int j = 0; j < usersToPay; j++) {
result.put(usersToPay.get(j).getId(), usersToPay.get(j));
}
}
countDownLatch.countDown();
}
}
@Scheudled
public void start() {
List<UserDto> users = restTemplate.get();
CountDownLatch countDownLatch = new CountDownLatch(5);
int pos = 0;
int userNumber = users.size();
List<Thread> workers = new ArrayList<>();
Map<Integer, UserDto> result = new ConcurrentHashMap<>();
for (int i = 0; i < threadsNumber; i++) {
workers.add(
new Thread(
new Worker(
users.sublist(i * userNumber, userNumber, result), // divide users to portions
countDownLatch
)
)
);
}
workers.forEach(Thread::start);
countDownLatch.await();
// здесь в мапе уникальные юзеры, которым надо вызвать метод pay их можно по примеру разделить опять на подгруппы
и так же запустить для них метод pay
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment