-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jose.lausuch
committed
Feb 12, 2017
1 parent
161efa4
commit bf00247
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/python | ||
|
||
import threading | ||
import time | ||
|
||
|
||
class myThread (threading.Thread): | ||
|
||
def __init__(self, threadID, name, counter): | ||
threading.Thread.__init__(self) | ||
self.threadID = threadID | ||
self.name = name | ||
self.counter = counter | ||
|
||
def run(self): | ||
print "Starting " + self.name | ||
# Get lock to synchronize threads | ||
threadLock.acquire() | ||
print_time(self.name, self.counter, 3) | ||
# Free lock to release next thread | ||
threadLock.release() | ||
|
||
|
||
def print_time(threadName, delay, counter): | ||
while counter: | ||
time.sleep(delay) | ||
print "%s: %s" % (threadName, time.ctime(time.time())) | ||
counter -= 1 | ||
|
||
threadLock = threading.Lock() | ||
threads = [] | ||
|
||
# Create new threads | ||
thread1 = myThread(1, "Thread-1", 1) | ||
thread2 = myThread(2, "Thread-2", 2) | ||
|
||
# Start new Threads | ||
thread1.start() | ||
thread2.start() | ||
|
||
# Add threads to thread list | ||
threads.append(thread1) | ||
threads.append(thread2) | ||
|
||
# Wait for all threads to complete | ||
for t in threads: | ||
t.join() | ||
print "Exiting Main Thread" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/python | ||
|
||
import Queue | ||
import threading | ||
import time | ||
|
||
exitFlag = 0 | ||
|
||
|
||
class myThread (threading.Thread): | ||
|
||
def __init__(self, threadID, name, q): | ||
threading.Thread.__init__(self) | ||
self.threadID = threadID | ||
self.name = name | ||
self.q = q | ||
|
||
def run(self): | ||
print "Starting " + self.name | ||
process_data(self.name, self.q) | ||
print "Exiting " + self.name | ||
|
||
|
||
def process_data(threadName, q): | ||
while not exitFlag: | ||
queueLock.acquire() | ||
if not workQueue.empty(): | ||
data = q.get() | ||
queueLock.release() | ||
print "%s processing %s" % (threadName, data) | ||
else: | ||
queueLock.release() | ||
time.sleep(1) | ||
|
||
threadList = ["Thread-1", "Thread-2", "Thread-3"] | ||
nameList = ["One", "Two", "Three", "Four", "Five"] | ||
queueLock = threading.Lock() | ||
workQueue = Queue.Queue(10) | ||
threads = [] | ||
threadID = 1 | ||
|
||
# Create new threads | ||
for tName in threadList: | ||
thread = myThread(threadID, tName, workQueue) | ||
thread.start() | ||
threads.append(thread) | ||
threadID += 1 | ||
|
||
# Fill the queue | ||
queueLock.acquire() | ||
for word in nameList: | ||
workQueue.put(word) | ||
queueLock.release() | ||
|
||
# Wait for queue to empty | ||
while not workQueue.empty(): | ||
pass | ||
|
||
# Notify threads it's time to exit | ||
exitFlag = 1 | ||
|
||
# Wait for all threads to complete | ||
for t in threads: | ||
t.join() | ||
print "Exiting Main Thread" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/python | ||
|
||
import thread | ||
import time | ||
|
||
# Define a function for the thread | ||
|
||
|
||
def print_time(threadName, delay): | ||
count = 0 | ||
while count < 5: | ||
time.sleep(delay) | ||
count += 1 | ||
print "%s: %s" % (threadName, time.ctime(time.time())) | ||
|
||
# Create two threads as follows | ||
try: | ||
thread.start_new_thread(print_time, ("Thread-1", 2, )) | ||
thread.start_new_thread(print_time, ("Thread-2", 4, )) | ||
except: | ||
print "Error: unable to start thread" | ||
|
||
while 1: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/python | ||
|
||
import threading | ||
import time | ||
|
||
exitFlag = 0 | ||
|
||
|
||
class myThread (threading.Thread): | ||
|
||
def __init__(self, threadID, name, counter): | ||
threading.Thread.__init__(self) | ||
self.threadID = threadID | ||
self.name = name | ||
self.counter = counter | ||
|
||
def run(self): | ||
print "Starting " + self.name | ||
print_time(self.name, self.counter, 5) | ||
print "Exiting " + self.name | ||
|
||
|
||
def print_time(threadName, delay, counter): | ||
while counter: | ||
if exitFlag: | ||
threadName.exit() | ||
time.sleep(delay) | ||
print "%s: %s" % (threadName, time.ctime(time.time())) | ||
counter -= 1 | ||
|
||
# Create new threads | ||
thread1 = myThread(1, "Thread-1", 1) | ||
thread2 = myThread(2, "Thread-2", 2) | ||
|
||
# Start new Threads | ||
thread1.start() | ||
thread2.start() | ||
|
||
print "Exiting Main Thread" |