Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 14th, 2013, 08:11 AM
 Yongsmith
Registered User

Join Date: Jun 2013
Posts: 1
Time spent in forums: 2 m 3 sec
Reputation Power: 0
How to use two threads to produce even and odd numbers?

I am going to compile a small python program in order to use Queue to produce a random with a thread. For example, using one thread to print odd number, while another thread to print even number.

import random
import time
from Queue import Queue

self.sharedata = queue

def run(self):
for i %2 == 1 in range(200):
self.sharedata.put(i)
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'

self.sharedata = queue

def run(self):

for i %2 == 0 in range(200):
print self.getName(),'got a value:',self.sharedata.get()
time.sleep(random.randrange(10)/10.0)
print self.getName(),'Finished'

#2
June 28th, 2013, 04:55 AM
 batouttahell
Registered User

Join Date: Jun 2013
Posts: 1
Time spent in forums: 20 m 13 sec
Reputation Power: 0
Take a look at this:

Code:
```import threading
from math import ceil
from random import random
from queue import Queue

def __init__(self, name, size, objQueue):
self.size = size
self.objQueue = objQueue

def run(self):
count = 0
while(count < self.size):
oddNumber = ceil((random() * 100))
if(oddNumber % 2 != 0):
print("Print Odd: " + str(oddNumber))
self.objQueue.put("Odd: " + str(oddNumber))
count += 1

def __init__(self, name, size, objQueue):
self.size = size
self.objQueue = objQueue

def run(self):
count = 0
while(count < self.size):
EvenNumber = ceil((random() * 100))
if(EvenNumber % 2 == 0):
print("Print Even: " + str(EvenNumber))
self.objQueue.put("Even: " + str(EvenNumber))
count += 1

objQueue = Queue(20)

odd.start()
even.start()

while not objQueue.empty():
print(objQueue.get())

if __name__ == "__main__":

Quote:
 Originally Posted by Yongsmith I am going to compile a small python program in order to use Queue to produce a random with a thread. For example, using one thread to print odd number, while another thread to print even number. Here is my codes, please offer me some advice: import threading import random import time from Queue import Queue class jishu (threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i %2 == 1 in range(200): print self.getName(),'adding',i,'to queue' self.sharedata.put(i) time.sleep(random.randrange(10)/10.0) print self.getName(),'Finished' # oushu thread class oushu(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i %2 == 0 in range(200): print self.getName(),'got a value:',self.sharedata.get() time.sleep(random.randrange(10)/10.0) print self.getName(),'Finished'

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > How to use two threads to produce even and odd numbers?