
Ayuda hilos
Publicado por Shai Jesus (1 intervención) el 20/09/2014 17:43:49
Tengo esteprograma en hilos y tengo el proble que al desplegar me mescla los mensajes de los hilos y no se como corregirlos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import threading
import time
class hilo(threading.Thread):
#el constructor no hace falta especificarlo ya que la clase lo hace por nosotros.
def __init__(self):
threading.Thread.__init__(self)
def run(self):
inicial=time.time()
print("ha iniciado el hilo 1")
for i in range(100):
print("\ncontador= ",i)
# time.sleep(0.6)
print("\nse ha terminado el hilo 1")
final=time.time()
total=final-inicial
print ("\nTiempo de ejecucion del hilo 1: "+str(total)+" segundos")
#el metodo run es donde se debe introducir el codigo que se ejecuta en segundo plano.
class hilo2(threading.Thread):
def run(self):
inicial=time.time()
print("\nha iniciado el hilo 2")
c=1
i=1
for n in range(2,1000):
for x in range(2,n):
if n%x == 0:
c=c+1
if c<2:
print("\nnumero primo",i,"= ",n)
i+=1
c=1
print("\nse ha terminado el hilo 2")
final=time.time()
total=final-inicial
print ("\nTiempo de ejecucion del hilo 2: "+str(total)+" segundos")
class hilo3(threading.Thread):
def run(self):
inicial=time.time()
print("\nha iniciado el hilo 3")
x=0
y=1
i=0
while x<10000000:
print("\nElmento ",i," de la serie de fibuncachi=",x,"\n")
aux=x
x=y
y=aux
y=y+x
i+=1
#time.sleep(0.1)
print("\nse ha terminado el hilo 3")
final=time.time()
total=final-inicial
print ("\nTiempo de ejecucion del hilo 3: "+str(total)+" segundos")
class hilo4(threading.Thread):
def run(self):
inicial=time.time()
print("\nha iniciado el hilo 4\n")
x=1
i=0
while x<1000:
x=7*i
print("\nMultiplo ",i," de 7= ",x)
i+=1
#time.sleep(0.3)
print("\nse ha terminado el hilo 4")
final=time.time()
total=final-inicial
print ("\nTiempo de ejecucion del hilo 4: "+str(total)+" segundos")
h1=hilo()
h2=hilo2()
h3=hilo3()
h4=hilo4()
h1.start()
h2.start()
h3.start()
h4.start()
Valora esta pregunta


0