Skip to content

Commit 93cf87c

Browse files
committed
.
.
1 parent a4ca281 commit 93cf87c

File tree

14 files changed

+223
-140
lines changed

14 files changed

+223
-140
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package j0019_synchronized;
22

3-
class ContadorIncorrecto {
3+
class Count {
44
private int count = 0;
55

6-
public void incrementar() {
6+
// Método sincronizado
7+
public synchronized void incrementar() {
78
count++;
89
}
910

@@ -12,32 +13,35 @@ public int getCount() {
1213
}
1314
}
1415

15-
public class WrongMethodWithoutSynchronizing {
16+
public class NoSync {
1617
public static void main(String[] args) {
17-
ContadorIncorrecto contadorIncorrecto = new ContadorIncorrecto();
18+
Count count = new Count();
1819

20+
// Crear múltiples hilos que incrementan el count
1921
Thread hilo1 = new Thread(() -> {
2022
for (int i = 0; i < 1000; i++) {
21-
contadorIncorrecto.incrementar();
23+
count.incrementar();
2224
}
2325
});
2426

2527
Thread hilo2 = new Thread(() -> {
2628
for (int i = 0; i < 1000; i++) {
27-
contadorIncorrecto.incrementar();
29+
count.incrementar();
2830
}
2931
});
3032

3133
hilo1.start();
3234
hilo2.start();
3335

36+
// Esperar a que ambos hilos terminen
3437
try {
3538
hilo1.join();
3639
hilo2.join();
3740
} catch (InterruptedException e) {
3841
e.printStackTrace();
3942
}
4043

41-
System.out.println("Valor final del contador: " + contadorIncorrecto.getCount());
44+
// Mostrar el valor del count
45+
System.out.println("Valor final del count: " + count.getCount());
4246
}
4347
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package j0019_synchronized_2;
2+
class Main {
3+
private static final Object monitor = new Object();
4+
private static boolean turn = true; // Control de turno
5+
6+
public static void main(String[] args) {
7+
Thread thread1 = new Thread(new CounterRunnable(true), "Thread-1");
8+
Thread thread2 = new Thread(new CounterRunnable(false), "Thread-2");
9+
10+
thread1.start();
11+
thread2.start();
12+
}
13+
14+
static class CounterRunnable implements Runnable {
15+
private final boolean isThread1;
16+
17+
public CounterRunnable(boolean isThread1) {
18+
this.isThread1 = isThread1;
19+
}
20+
21+
@Override
22+
public void run() {
23+
for (int i = 1; i <= 5; i++) {
24+
synchronized (monitor) {
25+
while (turn != isThread1) {
26+
try {
27+
monitor.wait();
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
System.out.println(Thread.currentThread().getName() + ": " + i);
33+
turn = !turn;
34+
monitor.notifyAll();
35+
}
36+
}
37+
}
38+
}
39+
}
+2-83
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package j0024_Array_Methods;
2+
23
public class Main {
3-
public static void main(String[] args) throws InterruptedException {
4+
public static void main(String[] args) {
45
// Ejemplo del método length
56
int[] array = {1, 2, 3, 4, 5};
67
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 {
2526
Class<?> arrayClass = array.getClass();
2627
System.out.println("Class of array: " + arrayClass); // Devuelve la clase de tiempo de ejecución de este objeto
2728

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-
}
10729

108-
// Espera a que los hilos terminen
109-
thread3.join();
110-
thread4.join();
11130
}
11231
}

intelliJ/src/j0025_Threads/Main.java

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package j0025_Threads;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
// Para los métodos notify(), notifyAll(), y wait(), necesitamos un objeto monitor
6+
Object monitor = new Object();
7+
8+
// Ejemplo del método notify()
9+
Thread thread1 = new Thread(() -> {
10+
synchronized (monitor) {
11+
try {
12+
System.out.println("Thread1 waiting...");
13+
monitor.wait(); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
14+
System.out.println("Thread1 notified!");
15+
} catch (InterruptedException e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
});
20+
21+
// Ejemplo del método notifyAll()
22+
Thread thread2 = new Thread(() -> {
23+
synchronized (monitor) {
24+
try {
25+
System.out.println("Thread2 waiting...");
26+
monitor.wait(); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
27+
System.out.println("Thread2 notified!");
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
});
33+
34+
thread1.start();
35+
thread2.start();
36+
37+
// Pause for a moment to ensure both threads are waiting
38+
try {
39+
Thread.sleep(1000);
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
44+
synchronized (monitor) {
45+
System.out.println("Main thread notifying all...");
46+
monitor.notifyAll(); // Despierta todos los hilos que están esperando en el monitor de este objeto
47+
}
48+
49+
// Espera a que los hilos terminen
50+
try {
51+
thread1.join();
52+
thread2.join();
53+
} catch (InterruptedException e) {
54+
e.printStackTrace();
55+
}
56+
57+
// Usar notify() en lugar de notifyAll() para despertar un solo hilo
58+
Thread thread3 = new Thread(() -> {
59+
synchronized (monitor) {
60+
try {
61+
System.out.println("Thread3 waiting...");
62+
monitor.wait(); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
63+
System.out.println("Thread3 notified!");
64+
} catch (InterruptedException e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
});
69+
70+
Thread thread4 = new Thread(() -> {
71+
synchronized (monitor) {
72+
try {
73+
System.out.println("Thread4 waiting...");
74+
monitor.wait(); // Hace que el subproceso actual espere hasta que otro subproceso invoque notify()
75+
System.out.println("Thread4 notified!");
76+
} catch (InterruptedException e) {
77+
e.printStackTrace();
78+
}
79+
}
80+
});
81+
82+
thread3.start();
83+
thread4.start();
84+
85+
// Pause for a moment to ensure both threads are waiting
86+
try {
87+
Thread.sleep(1000);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
}
91+
92+
synchronized (monitor) {
93+
System.out.println("Main thread notifying one...");
94+
monitor.notify(); // Despierta un único hilo que está esperando en el monitor de este objeto
95+
}
96+
97+
// Espera a que los hilos terminen
98+
try {
99+
thread3.join();
100+
thread4.join();
101+
} catch (InterruptedException e) {
102+
e.printStackTrace();
103+
}
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package j0026_Arrays_2D_3D;
2+
public class Array_2D{
3+
public static void main(String[] args) {
4+
int[][] matrix = new int[3][3];
5+
matrix[0][0] = 1;
6+
matrix[0][1] = 2;
7+
matrix[0][2] = 3;
8+
matrix[1][0] = 4;
9+
matrix[1][1] = 5;
10+
matrix[1][2] = 6;
11+
matrix[2][0] = 7;
12+
matrix[2][1] = 8;
13+
matrix[2][2] = 9;
14+
15+
for(int i=0; i<matrix.length; i++){
16+
for(int j=0; j<matrix[i].length; j++){
17+
System.out.print(matrix[i][j]);
18+
}
19+
System.out.println();
20+
}
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package j0026_Arrays_2D_3D;
2+
public class Array_3D {
3+
public static void main(String[] args) {
4+
int[][][] matriz = new int[3][3][3];
5+
6+
// Llenar la matriz tridimensional
7+
for (int i = 0; i < 3; i++) {
8+
for (int j = 0; j < 3; j++) {
9+
for (int k = 0; k < 3; k++) {
10+
matriz[i][j][k] = i + 1 + j * 3;
11+
}
12+
}
13+
}
14+
15+
// Imprimir la matriz tridimensional
16+
for (int i = 0; i < 3; i++) {
17+
for (int j = 0; j < 3; j++) {
18+
for (int s = 0; s < 3; s++) {
19+
System.out.print(matriz[s][j][i] + " ");
20+
}
21+
System.out.println();
22+
}
23+
System.out.println();
24+
}
25+
}
26+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package j0026_Arrays_2D_3D;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
int[][] doble = new int[3][3];
6+
for (int i = 0; i <3; i++) {
7+
for (int j = 0; j <3; j++) {
8+
doble[i][j] = i + 1 + j * 3;
9+
}
10+
}
11+
for (int i = 0; i <3; i++) {
12+
for (int j = 0; j <3; j++) {
13+
System.out.print(doble[j][i] + " ");
14+
}
15+
System.out.println();
16+
}
17+
}
18+
}

intelliJ/src/test/Animal.java

-8
This file was deleted.

intelliJ/src/test/Gato.java

-10
This file was deleted.

0 commit comments

Comments
 (0)