Untitled
unknown
java
a year ago
1.6 kB
2
Indexable
Never
public class NoSyncAppWithDaniel { private static int counter; public static void main(String[] args) { long startTime = System.currentTimeMillis(); // Tworzenie dwóch wątków Thread thread1 = new Thread(new IncrementTask()); Thread thread2 = new Thread(new IncrementTask()); Thread thread3 = new Thread(new IncrementTask()); Thread thread4 = new Thread(new IncrementTask()); // Uruchamianie wątków thread1.start(); thread2.start(); thread3.start(); thread4.start(); try { // Czekanie na zakończenie obu wątków thread1.join(); thread2.join(); thread3.join(); thread4.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Wartość zmiennej counter: " + counter); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println("Czas trwania: " + elapsedTime + " ms"); } // Zadanie inkrementacji zmiennej counter private static class IncrementTask implements Runnable { @Override public void run() { for (; counter < 1000; counter++) { try { Thread.sleep(50); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(counter); } } } }