1
1
package j0024_Array_Methods ;
2
+
2
3
public class Main {
3
- public static void main (String [] args ) throws InterruptedException {
4
+ public static void main (String [] args ) {
4
5
// Ejemplo del método length
5
6
int [] array = {1 , 2 , 3 , 4 , 5 };
6
7
System .out .println ("Length of array: " + array .length ); // Obtiene la longitud de la matriz
@@ -25,88 +26,6 @@ public static void main(String[] args) throws InterruptedException {
25
26
Class <?> arrayClass = array .getClass ();
26
27
System .out .println ("Class of array: " + arrayClass ); // Devuelve la clase de tiempo de ejecución de este objeto
27
28
28
- // Para los métodos notify(), notifyAll(), y wait(), necesitamos un objeto monitor
29
- Object monitor = new Object ();
30
-
31
- // Ejemplo del método notify()
32
- Thread thread1 = new Thread (() -> {
33
- synchronized (monitor ) {
34
- try {
35
- System .out .println ("Thread1 waiting..." );
36
- monitor .wait (); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
37
- System .out .println ("Thread1 notified!" );
38
- } catch (InterruptedException e ) {
39
- e .printStackTrace ();
40
- }
41
- }
42
- });
43
-
44
- // Ejemplo del método notifyAll()
45
- Thread thread2 = new Thread (() -> {
46
- synchronized (monitor ) {
47
- try {
48
- System .out .println ("Thread2 waiting..." );
49
- monitor .wait (); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
50
- System .out .println ("Thread2 notified!" );
51
- } catch (InterruptedException e ) {
52
- e .printStackTrace ();
53
- }
54
- }
55
- });
56
-
57
- thread1 .start ();
58
- thread2 .start ();
59
-
60
- // Pause for a moment to ensure both threads are waiting
61
- Thread .sleep (1000 );
62
-
63
- synchronized (monitor ) {
64
- System .out .println ("Main thread notifying all..." );
65
- monitor .notifyAll (); // Despierta todos los hilos que están esperando en el monitor de este objeto
66
- }
67
-
68
- // Espera a que los hilos terminen
69
- thread1 .join ();
70
- thread2 .join ();
71
-
72
- // Usar notify() en lugar de notifyAll() para despertar un solo hilo
73
- Thread thread3 = new Thread (() -> {
74
- synchronized (monitor ) {
75
- try {
76
- System .out .println ("Thread3 waiting..." );
77
- monitor .wait (); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
78
- System .out .println ("Thread3 notified!" );
79
- } catch (InterruptedException e ) {
80
- e .printStackTrace ();
81
- }
82
- }
83
- });
84
-
85
- Thread thread4 = new Thread (() -> {
86
- synchronized (monitor ) {
87
- try {
88
- System .out .println ("Thread4 waiting..." );
89
- monitor .wait (); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
90
- System .out .println ("Thread4 notified!" );
91
- } catch (InterruptedException e ) {
92
- e .printStackTrace ();
93
- }
94
- }
95
- });
96
-
97
- thread3 .start ();
98
- thread4 .start ();
99
-
100
- // Pause for a moment to ensure both threads are waiting
101
- Thread .sleep (1000 );
102
-
103
- synchronized (monitor ) {
104
- System .out .println ("Main thread notifying one..." );
105
- monitor .notify (); // Despierta un único hilo que está esperando en el monitor de este objeto
106
- }
107
29
108
- // Espera a que los hilos terminen
109
- thread3 .join ();
110
- thread4 .join ();
111
30
}
112
31
}
0 commit comments