
Ejercicios de Threads - java
Publicado por Fran (1 intervención) el 15/10/2021 10:31:15
Los procesos padres e hijos son simplemente procesos normales, el proceso padre tiene que crear a otro proceso, he hecho alguna prueba pero me salta la execpción:
xception in thread "Hilo Principal" java.lang.ClassCastException: class java.util.Date cannot be cast to class java.sql.Date (java.util.Date is in module java.base of loader 'bootstrap'; java.sql.Date is in module java.sql of loader 'platform')
at LanzarHilos.convertirAdate(LanzarHilos.java:60)
at LanzarHilos.main(LanzarHilos.java:34)
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
import java.util.Scanner;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class LanzarHilos {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
Date fecha = null;
String sfecha = "";
Thread.currentThread().setName("Hilo Principal"); // Renombramos el hilo principal
Hilo pp = new Hilo(); // inicializamos el proceso padre
/*
System.out.println("Ingrese la fecha de finalización_");
sfecha = sc.nextLine();
fecha = convertirAdate(sfecha);
pp.setFechafin(fecha); // pasamo la fecha al proceso padre
*/
pp.setNamed("pp");
pp.setCont(0);
System.out.println("Ingrese la fecha de finalización_");
sfecha = sc.nextLine();
fecha = convertirAdate(sfecha);
pp.setFechafin(fecha); // pasamo la fecha al proceso padre
pp.start();
pp.join();
System.out.println("Los dias restantes son: " + pp.getDias());
// pedimos al usuario la fecha actual
// generamos 3 hilos
/*h = new Hilo(); // creamos el hilo
h.setName("Proceso hijo ");
h.setPriority(7);
h.start(); // comienza el proceso
h.join(); // ver primero usaHiloJoin y volver despues aqui, espera a que termine el proceso
*/
}
public static Date convertirAdate(String fecha){
SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
Date fechaDate = null;
try {
fechaDate = (Date) formato.parse(fecha);
}
catch (ParseException ex)
{
System.out.println(ex);
}
return fechaDate;
}
}
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
91
92
93
94
95
96
97
98
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.util.Scanner;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZonedDateTime;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class Hilo extends Thread{
Scanner sc = new Scanner(System.in);
private Date fechafin;
private int dias;
private String named;
private int cont;
//crear getters y setters
public String getNamed() {
return named;
}
public void setNamed(String named) {
this.named = named;
}
public int getCont() {
return cont;
}
public void setCont(int cont) {
this.cont = cont;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(named.equalsIgnoreCase("pepe") && cont == 0) {
cont++;
System.out.println("EL proceso padre ha creado el proceso hijo");
Hilo ph = new Hilo();
ph.setNamed("ph");
ph.setFechafin(fechafin);
ph.start();
try {
ph.join();
dias = ph.dias; // sacacamos los dias de ph y lo almacenamos en pp
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fechafin != null && named.equalsIgnoreCase("ph")) {
Date fechaactual = new Date(System.currentTimeMillis());
int dias = (int) ((fechafin.getTime() - fechaactual.getTime()));
this.dias = dias;
}
}
public Date getFechafin() {
return fechafin;
}
public void setFechafin(Date fechafin) {
this.fechafin = fechafin;
}
public int getDias() {
return dias;
}
public void setDias(int dias) {
this.dias = dias;
}
}
- Actividad-1.2.zip(53,7 KB)
Valora esta pregunta


0