Gemeinsame Ressource und Mehrfachinstanziierung von Threads

Sonnenblume123

Aktives Mitglied
Hallo,
ich habe diesen Code gegeben:
Java:
public class Synch {
    public static int common = 0;

    public static class ThreadA extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }

        }
    }

    public static class ThreadB extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            common = 0;
            Thread a = new ThreadA();
            Thread b = new ThreadB();
            a.start();
            b.start();
           
            //a.join();
            //b.join(); mit try/catch behandeln
        }
    }
}

Die Aufgabe war, dass sichergestellt wird, dass in der for Schleife der main-Methode maximal eine Instanz der Klasse ThreadA und maximal eine Instanz der Klasse ThreadB nebenläufig ausgeführt werden.
Meine Lösung sieht ihr in den Kommentaren. Ich dachte nämlich, dass sich so das Problem lösen lässt, bevor ein neuer Schleifendurchlauf beginnt, wird der alte Thread beendet.
Stimmt ihr mir da zu?

Dann sollte noch ohne meine Anpassung mit join eine Semaphore verwendet, die sicherstellt, dass nur ein Thread auf die gemeinsame Variable common zugreift. Dazu meine Lösung:
Java:
import java.util.concurrent.Semaphore;

public class Synch2 {
    public static int common = 0;
    public static Semaphore sem = new Semaphore(1, true);

    public static class ThreadA extends Thread {
        public void run() {
            try {
                sem.acquire();
            } catch (InterruptedException e) {}
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
            sem.release();
        }
    }

    public static class ThreadB extends Thread {
        public void run() {
            try {
                sem.acquire();
            } catch (InterruptedException e) {}
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
            sem.release();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            common = 0;
            Thread a = new ThreadA();
            Thread b = new ThreadB();
            a.start();
            b.start();
        }
    }
}

Stimmt ihr mir da zu?

Vielen Dank im Voraus:) Und für die bisherige Hilfe in diesem Forum:D
 
X

Xyz1

Gast
Java:
public class Synch {
    public static int common = 0;

    public static class ThreadA extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }

        }
    }

    public static class ThreadB extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Semaphore se = new Semaphore(1, true);
        for (int i = 0; i < 100; i++) {
            se.acquire();
            common = 0;
            Thread a = new ThreadA();
            Thread b = new ThreadB();
            a.start();
            b.start();
            a.join();
            b.join();
            se.release();
            System.out.println("common : " + common);
        }
    }
}

Code:
common : 200
common : 200
common : 133
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 179
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 196
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200

Die Aufgabe war, dass sichergestellt wird, dass ....
Woher sind diese Aufgaben denn?
 
X

Xyz1

Gast
ohne meine Anpassung mit join
Ahhhhh.... überehen.... :
Java:
public class Synch {
    static Semaphore se = new Semaphore(2, true);
    public static int common = 0;

    public static class ThreadA extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
            se.release();
        }
    }

    public static class ThreadB extends Thread {
        public void run() {
            for (int i = 0; i < 100; i++) {
                common = common + 1;
            }
            se.release();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            se.acquire();
            se.acquire();
            System.out.println("common : " + common);
            common = 0;
            Thread a = new ThreadA();
            Thread b = new ThreadB();
            a.start();
            b.start();
            //a.join();
            //b.join();
            //
        }
    }
}

Code:
common : 0
common : 200
common : 188
common : 200
common : 183
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 177
common : 200
common : 200
common : 200
common : 200
common : 200
common : 191
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 185
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200
common : 200

So ist es 10000 %ig korrekt :eek:
 
X

Xyz1

Gast
@mrBrown misch Dich nicht ein
hier soll NICHT sichergestellt werden, dass korrekt gerechnet wird
das ist ein Unterschied! (wichtig)
 

mrBrown

Super-Moderator
Mitarbeiter
hier soll NICHT sichergestellt werden, dass korrekt gerechnet wird
das ist ein Unterschied! (wichtig)
Richtig - aber das richtige Rechnen ergibt sich dabei daraus, dass immer nur ein Thread auf die Variable zugreift. Und letzteres passiert bei dir nicht, du erlaubst ganz explizit 2 Threads zur selben Zeit.
das ist ein Unterschied! (wichtig)
 

mrBrown

Super-Moderator
Mitarbeiter
das ist meiner Meinung erfüllt....
Dort stehen zwei Aufgabenteile.
Der Teil, der völlig korrekt mit Join gelöst ist, und der Teil, der völlig korrekt mit Semaphore gelöst ist.

Wenn sich dein Code entgegen dem zitiertem und trotz der Nutzung von Semaphore auf die Lösung ohne Semaphore bezieht, solltest du das dazu schreiben...
 
X

Xyz1

Gast
ehrlich gesagt bin ich mir jetzt nicht mehr sicher, ob die Aufgabenstellung(en) konsistent sind.... bis später

Bearbeitung, also in sich schlüssig sind (gibts dafür kein Synonym)
 

Sonnenblume123

Aktives Mitglied
Also funktioniert der Teil mit join(), aber bei der Semaphore greift mehr als ein Thread auf die Variable common zu?

Wenn das stimmt, kann mir jemand sagen, was ich falsch gemacht hab?
 
X

Xyz1

Gast
Okay
Was ist gegeben und wie lautet die erste (Eingangs-) und zweite (Entwickelnde-) Aufgabenstellung dazu genau? :rolleyes:
 

mrBrown

Super-Moderator
Mitarbeiter
Ich sollte sicherstellen, dass bei der Semaphore nur ein Thread auf die Variable common zugreift.
Und das ist jetzt erfüllt oder? ^Verwirrt
In *deiner* Variante mit Semaphore ist das erfüllt. (Es können n Threads parallel laufen, aber immer nur einer greift auf dir Variabe zu)
In der Variante von DerWissende greifen zwei Threads parallel zu. (Es laufen im genau zwei Threads gleichzeitig, und beide können gleichzeitig zugreifen)
 

Ähnliche Java Themen

Neue Themen


Oben