diff --git a/Chapter11/Creating_a_Thread.java b/Chapter11/Creating_a_Thread.java index b3c357b..adb6735 100644 --- a/Chapter11/Creating_a_Thread.java +++ b/Chapter11/Creating_a_Thread.java @@ -23,7 +23,7 @@ public void run() { // Threads start executing here. } catch (InterruptedException exc) { System.out.println(thrdName + " interrupted."); } - + System.out.println(thrdName + " terminating."); } } @@ -33,6 +33,28 @@ public class Creating_a_Thread { public static void main(String[] args) { // TODO Auto-generated method stub + + System.out.println("Main thread starting."); + + // First, construct a MyThread object. + MyThread2023 myThread2023 = new MyThread2023("Child #1"); // Create a runnable object. + + // Next, construct a thread from that object. + Thread newThread = new Thread(myThread2023); // Construct a thread on that object. + + // Finally, start execution of the thread. + newThread.start(); // Start running the thread. + + for (int i = 0; i < 50; i++) { + System.out.print("."); + try { + Thread.sleep(100); + } catch (InterruptedException exc) { + System.out.println("Main thread interrupted."); + } + } + + System.out.println("Main thread ending."); } diff --git a/Chapter11/Creating_a_Thread_annotated.java b/Chapter11/Creating_a_Thread_annotated.java new file mode 100644 index 0000000..abe09f2 --- /dev/null +++ b/Chapter11/Creating_a_Thread_annotated.java @@ -0,0 +1,23 @@ +// To create a new thread, your program will either extend Thread or implement the Runnable interface. +class MyThread202310 implements Runnable{ + + public void run() { + + + } + +} + + +public class Creating_a_Thread_annotated { + + // From the main thread, you can create other threads. + public static void main(String[] args) { + System.out.println("Main thread starting."); + + // To create another thread from within the main thread, construct a MyThread object first. + + + } + +}