1
+ import java .util .Random ;
2
+ import java .util .concurrent .ExecutorService ;
3
+ import java .util .concurrent .Executors ;
4
+ import java .util .concurrent .TimeUnit ;
5
+
6
+ public class martinbohorquez {
7
+ public static void main (String [] args ) {
8
+ callbackExample ();
9
+ orderProcess ("Pizza Carbonara" );
10
+ orderProcess ("Pizza Margarita" );
11
+ orderProcess ("Pizza Pepperoni" );
12
+ orderProcess ("Pizza Barbacoa" );
13
+ }
14
+
15
+ private static void orderProcess (String dishName ) {
16
+ orderProcess (dishName , martinbohorquez ::confirmOrder , martinbohorquez ::orderReady , martinbohorquez ::orderDelivered );
17
+ }
18
+
19
+ private static void callbackExample () {
20
+ System .out .println ("Iniciando el programa..." );
21
+ Processor processor = new Processor ();
22
+ processor .process (message -> System .out .println ("Callback recibido: " + message ));
23
+ System .out .println ("Programa finalizado." );
24
+ }
25
+
26
+ public static void orderProcess (String dishName , ConfirmCallback confirmCallback ,
27
+ ReadyCallback readyCallback , DeliveredCallback deliveredCallback ) {
28
+ ExecutorService executor = Executors .newSingleThreadExecutor ();
29
+ executor .submit (() -> {
30
+ Random random = new Random ();
31
+ try {
32
+ confirmCallback .execute (dishName );
33
+ TimeUnit .SECONDS .sleep (random .nextInt (1 , 10 ));
34
+ readyCallback .execute (dishName );
35
+ TimeUnit .SECONDS .sleep (random .nextInt (1 , 10 ));
36
+ deliveredCallback .execute (dishName );
37
+ } catch (InterruptedException e ) {
38
+ throw new RuntimeException (e );
39
+ }
40
+ });
41
+ executor .shutdown ();
42
+ }
43
+
44
+ private static void confirmOrder (String dishName ) {
45
+ System .out .printf ("Tu pedido %s ha sido confirmado!%n" , dishName );
46
+ }
47
+
48
+ private static void orderReady (String dishName ) {
49
+ System .out .printf ("Tu pedido %s está listo!%n" , dishName );
50
+ }
51
+
52
+ private static void orderDelivered (String dishName ) {
53
+ System .out .printf ("Tu pedido %s ha sido entregado!%n" , dishName );
54
+ }
55
+
56
+ private interface Callback {
57
+ void execute (String message );
58
+ }
59
+
60
+ // Interfaces para los callbacks
61
+ public interface ConfirmCallback {
62
+ void execute (String dishName );
63
+ }
64
+
65
+ public interface ReadyCallback {
66
+ void execute (String dishName );
67
+ }
68
+
69
+ public interface DeliveredCallback {
70
+ void execute (String dishName );
71
+ }
72
+
73
+ private static class Processor {
74
+ public void process (Callback callback ) {
75
+ System .out .println ("Procesando..." );
76
+ try {
77
+ Thread .sleep (4000 );
78
+ } catch (InterruptedException e ) {
79
+ e .printStackTrace ();
80
+ }
81
+ callback .execute ("Proceso completo" );
82
+ }
83
+ }
84
+ }
0 commit comments