Threads Synchronisierung zwischen threads

lomi

Mitglied
Hallo zusammen,
ich versuche eine websocket anwendung zu implmentieren. Websocket Verbindung kann erstellt werden. ich wollte es nun in threads packen damit es besser synchronisiert ist dazu habe ich folgenden runnable geschrieben

Code:
public class YIOremoteDockHandler extends BaseThingHandler {
 private void establishconnectionfunction() {
        establishconnection = new Runnable() {
            @Override
            public void run() {

                try {
                    try {
                        uriyiodockwebsocketaddress = new URI("ws://" + localConfig.host + ":946");
                        yioremotedockactualstatus = YIOREMOTEDOCKHANDLESTATUS.AUTHENTICATION_PROCESS;

                    } catch (URISyntaxException e) {
                        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
                                "Initialize web socket failed");
                    }
                    try {
                        yioremoteDockHandlerwebSocketClient.start();
                    } catch (Exception e) {
                    }

                    yioremoteDockHandlerwebSocketClient.connect(yioremoteDockwebSocketClient,
                            uriyiodockwebsocketaddress, yioremoteDockwebSocketClientrequest);
                    yioremoteDockwebSocketClient.getLatch().await();
                    this.wait();
                    if (yioremoteDockwebSocketClient.getbooleanwebsocketconnected()) {
                        wait(10000);
                        if (yioremoteDockwebSocketClient.getbooleanauthenticationrequired()) {
                            yioremoteDockwebSocketClient.sendMessage(YIOREMOTEMESSAGETYPE.AUTHENTICATE,
                                    localConfig.accesstoken);
                            establishconnection.wait(10000);
                            if (yioremoteDockwebSocketClient.getbooleanauthenticationok()) {
                                yioremotedockactualstatus = YIOREMOTEDOCKHANDLESTATUS.AUTHENTICATED_COMPLETE;
                                logger.debug("authentication to YIO dock ok");
                                updateStatus(ThingStatus.ONLINE);
                                scheduler.notify();
                            } else {
                                updateState(GROUP_OUTPUT, YIODOCKSTATUS, UnDefType.UNDEF);
                                updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                                        "No connection to the websocket");
                                yioremotedockactualstatus = YIOREMOTEDOCKHANDLESTATUS.AUTHENTICATION_PROCESS_FAILED;
                            }
                        } else {
                            updateState(GROUP_OUTPUT, YIODOCKSTATUS, UnDefType.UNDEF);
                            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                                    "No connection to the websocket");
                            yioremotedockactualstatus = YIOREMOTEDOCKHANDLESTATUS.AUTHENTICATION_PROCESS_FAILED;
                        }
                    } else {
                        updateState(GROUP_OUTPUT, YIODOCKSTATUS, UnDefType.UNDEF);
                        updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                                "No connection to the websocket");
                        yioremotedockactualstatus = YIOREMOTEDOCKHANDLESTATUS.AUTHENTICATION_PROCESS_FAILED;
                    }
                } catch (IOException | InterruptedException e) {
                }

            }

        };
        establishconnectionthread = scheduler.schedule(establishconnection, 0, TimeUnit.SECONDS);
}

und die websocket class mit dem on connect welches das wait freigeben soll

Code:
@NonNullByDefault
@WebSocket

public class YIOremoteDockWebsocket {
@OnWebSocketConnect
    public void onConnect(Session session) {

        this.session = session;
        latch.countDown();
        yioremotedockhandler.establishconnectionthread.notify();
        booleanwebsocketconnected = true;

    }
    }

aber die synchronisation funkioniert nicht.....Wo st der Fehler???
 
Also beim Wettbewerb um den unleserlichsten Code bist du heute völlig außer Konkurrenz 😅


Auf den ersten Blick fehlen da synchronized-Blöcke. Vermutlich rufst du wait und notify auch auf unterschiedlichen Objekten auf.

Dann gibts mal wait, mal this.wait, und mal auf establishconnection.wait, was alles das selbe Objekt ist - keine Ahnung ob das Absicht ist, Unterschiede dabei sind zumindest immer auffällig. Außerdem mal mit und mal ohne timeout, wirkt zumindest auch merkwürdig.
 
Das kann gut sein, da ich es mir gerade selber erarbeite oder auch lerne zu programmieren.....


Wie synchronisiere ich richtig zwischen zwei Objekten gibt es da ein Beispiel??
 
Auf den ersten Blick
... ist das eine andere Sprache. Zuerst dachte ich an irgendeine Verschlüsselung.

Wie synchronisiere ich richtig zwischen zwei Objekten gibt es da ein Beispiel??
 
Wenn ich das richtig sehe, ist das Openhab. Das als Anfang zu nehmen ist mutig 🙂 Weil da hast du inhärentes Multithreading vom Framework
 

Zurück
Oben