import threading
import time
lock = threading.Lock()
def high_priority():
while True:
with lock:
print("High priority thread acquired lock")
time.sleep(0.2)
def low_priority():
while True:
with lock:
print("Low priority thread acquired lock") # May never happen!
time.sleep(0.1)
t1 = threading.Thread(target=high_priority, daemon=True)
t2 = threading.Thread(target=low_priority, daemon=True)
t1.start()
t2.start()
time.sleep(1)
< /code>
Ich habe den Code 23 Mal über 23 Mal ausgeführt, bevor er Low_Priority den Sperre erhalten. >
Wenn innerhalb des 1 -Sekunden -Schlafes des Hauptskripts Low_Priority Thread das Sperre erhält < /li>
Wie oft liefert Low_Priority Thread die Sperre < /li>
< /ul>
Die Experimente scheinen jedoch eine lange Zeit zu erfordern, also suche ich zuerst nach einer Theorie. : < /p>
➜ deadlock python deadlock_priority.py
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
➜ deadlock python deadlock_priority.py
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
➜ deadlock python deadlock_priority.py
High priority thread acquired lock
High priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
Low priority thread acquired lock
Low priority thread acquired lock
Low priority thread acquired lock
< /code>
Wird die Priorität gemäß Reihenfolge der Startmethode oder durch Schlaflänge in den Threads bestimmt? < /p>
t1.start()
t2.start()
< /code>
Ich habe versucht, weniger auf Low_Priority zu schlafen, um ihm eine höhere Chance zu übernehmen. Ist das Wunschdenken? Ich vermute Zeit. (Das heißt in einer bestimmten Anzahl von Läufen, ich kann sehen, dass Low_Priority die Sperre erhalten. p>
Ich habe gerade den Code geschrieben, um den Thread -Wettbewerb ohne Anwendungsfall zu studieren, aber die Intuition sagt mir, dass es eine Verwendung gibt. /p>
Ich habe mich entschlossen, den Sleeps außerhalb des Lock -Kontextmanagers zufällig hinzuzufügen, und irgendwie erhält Low_Priority das Schloss viel öfter? < /p>
def high_priority():
while True:
time.sleep(0.0001)
with lock:
print("High priority thread acquired lock")
time.sleep(0.2)
def low_priority():
while True:
time.sleep(0.0001)
with lock:
print("Low priority thread acquired lock")
time.sleep(0.1)
< /code>
Ausgabe von 1st 3 läuft: < /p>
➜ deadlock python deadlock_sleep.py
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
➜ deadlock python deadlock_sleep.py
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
➜ deadlock python deadlock_sleep.py
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
Low priority thread acquired lock
High priority thread acquired lock
time.sleep(1) < /code> Ich habe den Code 23 Mal über 23 Mal ausgeführt, bevor er Low_Priority den Sperre erhalten. > Wenn innerhalb des 1 -Sekunden -Schlafes des Hauptskripts Low_Priority Thread das Sperre erhält < /li> Wie oft liefert Low_Priority Thread die Sperre < /li> < /ul> Die Experimente scheinen jedoch eine lange Zeit zu erfordern, also suche ich zuerst nach einer Theorie. : < /p> ➜ deadlock python deadlock_priority.py High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock ➜ deadlock python deadlock_priority.py High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock ➜ deadlock python deadlock_priority.py High priority thread acquired lock High priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock Low priority thread acquired lock Low priority thread acquired lock Low priority thread acquired lock < /code> Wird die Priorität gemäß Reihenfolge der Startmethode oder durch Schlaflänge in den Threads bestimmt? < /p> t1.start() t2.start() < /code> Ich habe versucht, weniger auf Low_Priority zu schlafen, um ihm eine höhere Chance zu übernehmen. Ist das Wunschdenken? Ich vermute Zeit. (Das heißt in einer bestimmten Anzahl von Läufen, ich kann sehen, dass Low_Priority die Sperre erhalten. p> Ich habe gerade den Code geschrieben, um den Thread -Wettbewerb ohne Anwendungsfall zu studieren, aber die Intuition sagt mir, dass es eine Verwendung gibt. /p> Ich habe mich entschlossen, den Sleeps außerhalb des Lock -Kontextmanagers zufällig hinzuzufügen, und irgendwie erhält Low_Priority das Schloss viel öfter? < /p> def high_priority(): while True: time.sleep(0.0001) with lock: print("High priority thread acquired lock") time.sleep(0.2)
def low_priority(): while True: time.sleep(0.0001) with lock: print("Low priority thread acquired lock") time.sleep(0.1) < /code> Ausgabe von 1st 3 läuft: < /p> ➜ deadlock python deadlock_sleep.py High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock ➜ deadlock python deadlock_sleep.py High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock ➜ deadlock python deadlock_sleep.py High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock Low priority thread acquired lock High priority thread acquired lock [/code]
Ich muss eine Aufgabe in festen Intervallen (z. B. jede Stunde) in einer C# -Anwendung ausführen. Was sind die besten Möglichkeiten, dies zu erreichen? Gibt es einen besseren Ansatz für die...
Ich habe einen ausführbaren Thread (Objekt) in mainactivity.kt onCreate wie folgt.
runnable = Runnable {
// Code to run in the background thread
val myURL = mydomain.com/myjson.php
thejsonstr =...