Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.uz.instrukcja01.zad02;
- public class LicznikWatkow extends Thread {
- private int wartoscKrytyczna;
- public int policzMnie () {
- wartoscKrytyczna += 1;
- wartoscKrytyczna--;
- return wartoscKrytyczna;
- }
- }
- public class WatekLiczacy extends Thread{
- private LicznikWatkow licznikWatkow; private int count;
- public WatekLiczacy(String name, LicznikWatkow licznikWatkow, int count) {
- super(name); this.licznikWatkow = licznikWatkow;
- this.count = count;
- }
- public void run() {
- int wynik = 0;
- for(int i=0; i< count; i++) {
- wynik = licznikWatkow.policzMnie();
- if(wynik != 0) break;
- }
- System.out.println(Thread.currentThread().getName() + " konczy wynikiem " + wynik);
- }
- }
- public class TestWatkow {
- public static void main(String[] args) {
- int tnum = Integer.parseInt(args[0]);
- int count = Integer.parseInt(args[1]);
- LicznikWatkow licznik = new LicznikWatkow();
- WatekLiczacy[] thread = new WatekLiczacy[tnum];
- for (int i = 0; i<tnum; i++)
- thread[i] = new WatekLiczacy("Watek" + (i+1), licznik, count);
- try{
- for (int i=0; i< tnum; i++) {thread[i].join();}
- } catch (InterruptedException exc) { System.exit(1); }
- System.out.println("Koniec programu");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement