Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab1;
- import java.util.LinkedList;
- import java.util.Vector;
- public class Main extends Thread {
- public static Counter counter = new Counter();
- public static final int threadCount = 2;
- public static final int loopCount = 5000;
- public static void main(String[] args) throws InterruptedException {
- Vector<Main> threads = new Vector<>();
- for(int i = 0; i < threadCount; i++){
- threads.add(new Main());
- threads.get(i).start();
- }
- for(int i = 0; i < threadCount; i++){
- threads.get(i).join();
- }
- System.out.println("done");
- counter.print();
- }
- public void run(){
- for(int i = 0; i < loopCount; i++){
- counter.increment();
- counter.decrement();
- }
- //counter.print();
- }
- }
- package lab1;
- public class Counter {
- private int value = 0;
- public void increment(){
- value++;
- }
- public void decrement(){
- value--;
- }
- public void print(){
- System.out.println(value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement