Thread State and Thread Class Methods

20. Using Java Awt and Java Applet, write a program to draw a pie chart according to the model in the following figure:


21. Using Java Awt, Java Applet and mouse events MouseListener write a program to draw 4 squares according to the following pattern, so that when you click on a square, it will change color randomly.


22. Using Java Awt, Java Applet and MouseListener mouse events, write a program to draw a human face according to the model in the image below, so that when you click on Smile, the human face will be smile (yellow background and smiling mouth), and when you click on Sad, the face will be sad (brown background, crooked mouth).


23. Using Java Awt, Java Applet draws 20 lines of Java text on the screen forming a circle with color gradually changing from red to blue according to the pattern in the following image:



24. Using Java Awt, Java Applet draw a target (red and white concentric circles) according to the pattern in the following image:


25. Using Java Awt, Java Applet, write a program to draw blue lines on a white background around a fixed center according to the pattern in the following image:


26. Using Java Awt, Java Applet, write a program to draw pink rectangles on a white background according to the pattern in the following image:

27. Using Java Awt, Java Applet, write a program to draw a chess board with red and black squares according to the pattern in the following image:



CHAPTER 6: MULTITHREADING PROGRAMMING

6.1. Introduction to threaded programming

Thread is a unique property of Java. It is the smallest unit of executable code that can perform a certain task. Java language, Java virtual machine both are threaded systems.

6.2. Threading and multithreading

Java supports multithreading, it is capable of working with multiple threads. An application can contain multiple threads. Each thread is assigned a specific task, they are executed concurrently with other threads.

Multithreading minimizes system idle time. This allows you to write highly efficient programs with maximum CPU utilization. Each part of

The program is called a thread, each thread defines a way of execution. This is a special case of multitasking.

In multitasking, multiple programs run concurrently, each with at least one thread in it. A single processor executes all these programs. The programs are thus executed as if they were concurrent, when in reality the processor switches back and forth between the programs.

6.3. Creating and managing streams

When Java programs are executed, the main thread is already executing.

The two important elements in the main flow are:

Sub-streams will be created from it.

It is the last thread to finish its execution. As soon as the main thread stops executing, the program is terminated .

Although the main thread is created automatically with program execution, it can be controlled through a thread object.

Streams can be created in two ways:

Declare the class as a subclass of the Thread class, and the run() method of the Thread class needs to be overridden.

Example 6.1: Create a subclass of Thread class

class Mydemo extends Thread

{


public void run()

{


//execute

}

}

Declare a class that implements the Runnable interface. Then define the run() method.

class MyDemo implements Runnable

{


public void run()

{


//execute

}

}

Example 6.2: Show how the main flow is controlled

import java.io.*;

public class Mythread extends Thread

{


public static void main(String args[])

{


Thread t = Thread.currentThread(); System.out.println("The current Thread is :" + t); t.setName("MyJavaThread"); System.out.println("The thread is now named: " + t); try{

for(int i = 0; i <3;i++)

{


System.out.println(i); Thread.sleep(1500);

}

}


catch(InterruptedException e)

{


System.out.println("Main thread interupted");

}

}

}


The following figure shows the output of the above program.

Figure 6.1. Result of executing example 6.2

In the output above


[main, 5 , main]

The thread group it depends on


Priority is set by JVM


Name of the stream


Each thread in a Java program is given a priority. The Java virtual machine never changes the priority of a thread. The priority remains constant until the thread is terminated.

Each thread has a priority value that ranges from Thread.MIN_PRIORITY (=1), to Thread.MAX_PRIORITY (=10). Each thread belongs to a thread group, and each thread group has its own priority. Each thread has a default priority value Thread.NORM_PRIORITY of 5. New threads inherit the priority of the thread that created them.

The Thread class has several constructors, two of which are mentioned below:

public Thread(String threadname)

The structure of a thread is named “threadname”

public Thread()

A thread constructs a thread with the name “Thread, concatenated with a number; for example Thread-1, Thread-2, etc…

The program starts the thread execution by calling the start() method of the Thread class. This method will call the run() method, where the job definition is

execute. This method can be overridden in a subclass of the Thread class, or in an object implementing the Runnable interface.

6.4. Thread Life Cycle


Figure 6.2. Stream life cycle

6.5. Thread state and methods of Thread class

A newly created thread is in the 'born' state. The thread does not start running immediately after it is created. It waits for its own start() method to be called. from there it is in the “ready to run” state. The thread enters the “running” state when the system grants it access to the processor.

The sleep() method can be used to temporarily stop the execution of a thread. The thread becomes available after the sleep() method finishes. A thread in the 'sleeping' state does not use the processor. A thread enters the 'waiting' state when a thread calls the wait() method.

When other threads have finished using the object, calling the notify() method, the thread returns to the “ready” state. A thread enters the “blocked” state when it is performing input/output operations. It enters the “ready” state when the input/output methods it is waiting for are completed. A thread enters the “dead” state after its run() method has finished executing, or when its stop() method is called.

Besides the methods mentioned above, the Thread class also has the following methods:



Method

Purpose

enumerate(Thread t)

Copies all current threads into the specified array.

Maybe you are interested!

define a thread group, and its subgroups.

getName()

Returns the name of the stream

isAlive()

Returns True, if the thread is still alive

getPriority()

Returns the thread priority

setName(String name)

Name the stream.

join()

Wait until the thread dies.

isDaemon()

Check if the thread is a daemon thread

setDeamon(boolean on)

Mark the thread as background or

resume()

Resume execution from suspended state

sleep()

Stops thread execution for a period of time.

start()

Call the run() method to start a thread.


Table 6.1. Methods of a thread class

Round-robin scheduling refers to threads with the same priority. When threads with the same priority execute at the same time, round-robin processor time sharing is automatically applied.

The latest version of Java does not support the Thread.suspend(), Thread.resume(), and Thread.stop() methods, because the methods create a deadlock risk.

6.6. Stream Schedule

Most Java programs are multithreaded. But the CPU is only capable of executing one thread at a time. When more than one thread with the same priority is executing at the same time, the programmer, or the Java virtual machine, or the operating system ensures that the CPU is shared between them. This is called thread control (thread scheduling).

No Java virtual machine has a specific mechanism for thread control. Some Java environments support time-sharing. Here, each thread gets a small portion of the processor time, called a quantum. The thread can execute its task during the allocated time period. After this time period, the thread must suspend its execution, even if it has not completed it. The next thread with the same priority will use the processor for the next time period. Java controls the time-sharing among all threads with the same priority.

Comment


Agree Privacy Policy *