Page 154 - CTS - CSA TP - Volume 2
P. 154
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 101 : Create and run a thread
Objectives
At the end of this exercise you shall be able to
• know the creation and execution of threads in Java
• develop Java programs using Super class and Sub class.
Requirements
Tools/Materials
• PC / laptop with windows OS
• SDK software
• Test editor (Visual studio/ subline/ notepad)
Procedure
In Java, a thread represents a single flow of execution within a program. Threads allow programs to perform
multiple tasks concurrently, making it possible to execute multiple pieces of code simultaneously.
TASK 1: Creating and activating a thread
public class MyThread1
{
// Main method
public static void main(String argvs[])
{
// creating an object of the Thread class using the constructor Thread(String name)
Thread t= new Thread(“My first thread”);
// the start() method moves the thread to the active state
t.start();
// getting the thread name by invoking the getName() method
String str = t.getName();
System.out.println(str);
}
}
Explanation:
Step 1: Develop the above code
• This code defines a class named MyThread1.
• The main method is the entry point of the program.
139
CITS : IT & ITES - Computer Software Application - Exercise 100