Lesson 4.2: Advanced Programming Logic Flashcards
What are the two distinct types of multitasking? Define each.
There are two distinct types of multitasking: process-based and thread-based. A process is، in essence، a program that is executing. Thus، process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example، it is process-based multitasking that allows you to run the Java compiler at the same time you are using a text editor or browsing the Internet. In process-based multitasking، a program is the smallest unit of code that can be dispatched by the scheduler. In a thread-based multitasking environment، the thread is the smallest unit of dispatchable code. This means that a single program can perform two or more tasks at once. For instance، a text editor can be formatting text at the same time that it is printing، as long as these two actions are being performed by two separate threads. Although Java programs make use of process-based multitasking environments، process-based multitasking is not under the control of Java. Multithreaded multitasking is.
What are the six states a thread can be in? What is synchronization in thread-based multitasking?
A thread can be in one of several states. It can be running. It can be ready to run as soon as it gets CPU time. A running thread can be suspended، which is a temporary halt to its execution. It can later be resumed. A thread can be blocked when waiting for a resource. A thread can be terminated، in which case its execution ends and cannot be resumed. Along with thread-based multitasking comes the need for a special type of feature called synchronization، which allows the execution of threads to be coordinated in certain well-defined ways. Java has a complete subsystem devoted to synchronization.
What is the name of a thread that all processes have?
All processes have at least one thread of execution، which is usually called the main thread، because it is the one that is executed when your program begins.
What are the two ways you can a runnable thread object?
Java defines two ways in which you can create a runnable object: You can implement the Runnable interface. You can extend the Thread class.
What is the run() thread and how do you use it?
You can construct a thread on any object that implements the Runnable interface. Runnable defines only one method called run( )، which is declared like this: public void run() Inside run( )، you will define the code that constitutes the new thread. It is important to understand that run( ) can call other methods، use other classes، and declare variables just like the main thread. The only difference is that run( ) establishes the entry point for another، concurrent thread of execution within your program. This thread will end when run( ) returns.
After you create a class that implements Runnable، what do you do?
After you have created a class that implements Runnable، you will instantiate an object of type Thread on an object of that class. Thread defines several constructors.
What is the Thread(Runnable threadOb) and how do you start it?
Thread(Runnable threadOb) In this constructor، threadOb is an instance of a class that implements the Runnable interface. This defines where execution of the thread will begin. Once created، the new thread will not start running until you call its start( ) method، which is declared within Thread. In essence، start( ) executes a call to run( ). The start( ) method is shown here: void start( )
what does the sleep() method do and what is it’s general form?
The sleep( ) method causes the thread from which it is called to suspend execution for the specified period of milliseconds. Its general form is shown here: static void sleep(long milliseconds) throws InterruptedException The number of milliseconds to suspend is specified in milliseconds. This method can throw an InterruptedException. Thus، calls to it must be wrapped in a try block. The sleep( ) method also has a second form، which allows you to specify the period in terms of milliseconds and nanoseconds if you need that level of precision. In run( )، sleep( ) pauses the thread for 400 milliseconds each time through the loop. This lets the thread run slow enough for you to watch it execute.
Give me an example of code that creates a new thread and starts running it:
Here is an example that creates a new thread and starts it running:
When does a multithreaded program stop executing?
In a multithreaded program، you often will want the main thread to be the last thread to finish running. As a general rule، a program continues to run until all of its threads have ended. Thus، having the main thread finish last is not a requirement. It is، however، often a good practice to follow
Why is it not necessary for a thread to store it’s own name? What is the code fragment to work around this? What are the two main advantages the work around?
However، there is no need for MyThread to store the name of the thread since it is possible to give a name to a thread when it is created. To do so، use this version of Thread’s constructor: Thread(Runnable threadOb، String name) 386Here، name becomes the name of the thread. You can obtain the name of the thread by calling getName( ) defined by Thread. Its general form is shown here: final String getName( ) Giving a thread a name when it is created provides two advantages. First، there is no need for you to use a separate variable to hold the name because Thread already provides this capability. Second، the name of the thread will be available to any code that holds a reference to the thread.
How do you set the name of a thread after it has been created?
You can set the name of a thread after it is created by using setName( )، which is shown here: final void setName(String threadName) Here، threadName specifies the new name of the thread.
What are the two thread variations that can make a thread more convenient to use?
there are two variations that can، depending on the situation، make MyThread more convenient to use. First، it is possible for the MyThread constructor to create a Thread object for the thread، storing a reference to that thread in an instance variable. With this approach، the thread is ready to start as soon as the MyThread constructor returns. You simply call start( ) on the Thread instance encapsulated by MyThread. The second variation offers a way to have a thread begin execution as soon as it is created. This approach is useful in cases in which there is no need to separate thread creation from thread execution. One way to accomplish this for MyThread is to provide a static factory method that: 1. creates a new MyThread instance، 2. calls start( ) on the thread associated with that instance، 3. and then returns a reference to the newly created MyThread object. With this approach، it becomes possible to create and start a thread through a single method call. This can streamline the use of MyThread، especially in cases in which several threads must be created and started.
What are factory methods?
In general، a factory method is a method that returns an object of a class. Typically، factory methods are static methods of a class. Factory methods are useful in a variety of situations. createAndStart( )، a factory method enables an object to be constructed and then set to some specified state prior to being returned to the caller. Another type of factory method is used to provide an easy-to-remember name that indicates the variety of object that is being constructed.
Try This 11-1 Extending Thread
// This code has been untest and will need it before finalization. // From page 389 in Java: A Beginner’s Guide by Herbert Schildt Implementing Runnable is one way to create a class that can instantiate thread objects. Extending Thread is the other. In this project، you will see how to extend Thread by creating a program functionally similar to the UseThreads program shown at the start of this chapter. When a class extends Thread، it must override the run( ) method، which is the entry point for the new thread. It must also call start( ) to begin execution of the new thread. It is possible to override other Thread methods، but doing so is not required. 1. Create a file called ExtendThread.java. Begin this file with the following lines: —————————-/* Try this 11-1 Extend Thread. / class MyThread extends Thread { —————————-Notice that MyThread now extends Thread instead of implementing Runnable. 2. Add the following MyThread constructor: —————————-// Construct a new thread. MyThread(String name) { super(name); // name thread } —————————-Here، super is used to call this version of Thread’s constructor: Thread(String threadName) Here، threadName specifies the name of the thread. As explained previously، Thread provides the ability to hold a thread’s name. Thus، no instance variable is required by MyThread to store the name. 3. Conclude MyThread by adding the following run( ) method: —————————-// Entry point of thread. public void run() { System.out.println(getName() + “ starting.”); try { for(int count=0; count < 10; count++) { Thread.sleep(400); System.out.println(“In “ + getName() + “، count is “ + count); } } catch(InterruptedException exc) { System.out.println(getName() + “ interrupted.”); } System.out.println(getName() + “ terminating.”); —————————-Notice the calls to getName( ). Because ExtendThread extends Thread، it can directly call all of Thread’s methods، including the getName( ) method. 4. Next، add the ExtendThread class shown here: —————————-class ExtendThread { public static void main(String args[]) { System.out.println(“Main thread starting.”); MyThread mt = new MyThread(“Child #1”); mt.start(); 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.”); } } —————————-In main( )، notice how an instance of MyThread is created and then started with these two lines: —————————-MyThread mt = new MyThread(“Child #1”); Mt.start(); —————————-Because MyThread now implements Thread، start( ) is called directly on the MyThread instance، mt. 5. Here is the complete program. Its output is the same as the UseThreads example، but in this case، Thread is extended rather than Runnable being implemented. —————————-/ Try this 11-1 Extend Thread. */ class MyThread extends Thread { // Construct a new thread. MyThread(String name) { super(name); // name thread } // Entry point of thread. public void run() { System.out.println(getName() + “ starting.”); try { for(int count=0; count < 10; count++) { Thread.sleep(400); System.out.println(“In “ + getName() + “، count is “ + count); } } catch(InterruptedException exc) { System.out.println(getName() + “ interrupted.”); } System.out.println(getName() + “ terminating.”); } } class ExtendThread { public static void main(String args[]) { System.out.println(“Main thread starting.”); MyThread mt = new MyThread(“Child #1”); mt.start(); 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.”); } } —————————6. When extending Thread، it is also possible to include the ability to create and start a thread in one step by using a static factory method، similar to that used by the ThreadVariations program shown earlier. To try this، add the following method to MyThread: —————————-public static MyThread createAndStart(String name) { MyThread myThrd = new MyThread(name); myThrd.start(); return myThrd; } —————————-As you can see، this method creates a new MyThread instance with the specified name، calls start( ) on that thread، and returns a reference to the thread. To try createAndStart( )، replace these two lines in main( ): —————————-System.out.println(“Main thread starting”); MyThread mt = new MyThread(“Child #1”); —————————-with this line: —————————-MyThread mt = MyThread.createAndStart(“Child #1”); —————————-After making these changes، the program will run the same as before، but you will be creating and starting the thread using a single method call.