Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. public class Solution {
  2. public static byte threadCount = 3;
  3. static List<Thread> threads = new ArrayList<Thread>(threadCount);
  4.  
  5. public static void main(String[] args) throws InterruptedException {
  6. initThreadsAndStart();
  7. Thread.sleep(3000);
  8. ourInterruptMethod();
  9. }
  10.  
  11. public static void ourInterruptMethod() {
  12. //add your code here - добавь код тут
  13. for(Thread y:threads)
  14. {
  15. y.interrupt();
  16. }
  17. }
  18.  
  19. private static void initThreadsAndStart() {
  20. Water water = new Water("water");
  21. for (int i = 0; i < threadCount; i++) {
  22. threads.add(new Thread(water, "#" + i));
  23. }
  24.  
  25. for (int i = 0; i < threadCount; i++) {
  26. threads.get(i).start();
  27. }
  28. }
  29.  
  30. public static class Water implements Runnable {
  31. private String sharedResource;
  32.  
  33. public Water(String sharedResource) {
  34. this.sharedResource = sharedResource;
  35. }
  36.  
  37. public void run() {
  38. //fix 2 variables - исправь 2 переменных
  39. boolean isCurrentThreadInterrupted = Thread.currentThread().isInterrupted();
  40. String threadName = Thread.currentThread().getName();
  41.  
  42. try {
  43. while (!isCurrentThreadInterrupted) {
  44.  
  45. System.out.println("Объект " + sharedResource + ", нить " + threadName);
  46. Thread.sleep(1000);
  47. }
  48. } catch (InterruptedException e) {
  49. }
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement