Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.plaf.basic.BasicEditorPaneUI;
- public class Main {
- public static void main(String[] args) {
- Brojac b = new Brojac();
- MyThread t1 = new MyThread(1, b);
- MyThread t2 = new MyThread(2, b);
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- }
- catch (InterruptedException ie) {
- System.out.println(ie);
- }
- System.out.println(b.getBr());
- }
- }
- class MyThread extends Thread {
- private int id;
- Brojac b;
- public MyThread(int _id, Brojac _b) {
- id = _id;
- b = _b;
- }
- @Override
- public void run() {
- for(int i = 0; i < 20; ++i) {
- // System.out.println("Thread " + id + " " + i);
- b.zgolemi_brojac();
- // zemi ja vrednosta na b
- // zgolemi ja vrednosta T1, T2
- // vrati ja vrednost
- }
- }
- }
- class Brojac {
- private int br = 0;
- public void zgolemi_brojac() {
- br++;
- try {
- Thread.sleep(5);
- }
- catch (InterruptedException ie) {
- System.out.println(ie);
- }
- }
- public int getBr() {
- return br;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement