OOPM JAVA - Create multiple threads to print $*$*$*$*$*$*.

 Experiment No. 14 

Aim: Write a program to create multiple threads to print $*$*$*$*$*$*.

Objective: To make aware of multithreading concept and use of thread class for creating different threads of a program. 

Outcome: Students implemented multithreading by inheriting the class Thread.

Theory:

Multithreading in java language is a process of executing many threads simultaneously.

Thread is a lightweight and smallest unit of processing. For multitasking, multiprocessing and multithreading are used.But multithreading is used more than multiprocessing because threads share common area for memory. 

Commonly used Constructors of Thread class:

1. Thread()

2. Thread(String name)

3. Thread(Runnable r)

4. Thread(Runnable r, String name)


Commonly used methods of Thread class:

  1. public void run(): It is used for performing action for a thread.
  2. public void start(): It starts the execution of the thread.JVM and calls the run() method on the thread.
  3. public void sleep(long miliseconds): It makes the currently executing thread to sleep for the specified number of milliseconds.
  4. public void join():It waits for a thread to die.
  5. public void join(long miliseconds):It waits for a thread to die for the specified miliseconds.
  6. public int getPriority():It returns the priority of the thread.
  7. public int setPriority(int priority):It changes the priority of the thread.
  8. public String getName():It returns the name of the thread.
  9. public void setName(String name):It changes the name of the thread.
  10. public Thread currentThread():It returns the reference of currently executing thread.
  11. public int getId():It returns the id of the thread.
  12. public Thread.State getState():It returns the state of the thread.
  13. public boolean isAlive():It tests if the thread is alive.
  14. public void yield():It causes the currently executing thread object to temporarily pause and allow other threads to execute.
  15. public void suspend():It is used to suspend the thread(depricated).
  16. public void resume():It is used to resume the suspended thread(depricated).
  17. public void stop():It is used to stop the thread(depricated).
  18. public boolean isDaemon():It tests if the thread is a daemon thread.
  19. public void setDaemon(boolean b):It marks the thread as daemon or user thread.
  20. public void interrupt():It interrupts the thread.
  21. public boolean isInterrupted():It tests if the thread has been interrupted.
  22. public static boolean interrupted():It tests if the current thread has been interrupted.

Algorithm:

1 Create a class ThreadA which is subclass of builtin class Thread with member

    function run( )

2 In procedure run( ) 

    2.1 Print "thread A starded"

    2.2 for i=l to 5

        2.1Print "$”

        2.2 sleep(800)

        2.3 i++

3 Create a class ThreadB which is subclass of builtin class Thread with member

    function run( )

4 In procedure run( )

    4. 1 Print "thread B starded"

        4.2 for i=l to 5

         4.2.1Print "*”

               4.2.2 sleep(1000)

    4.2.3 i++

5 Create a class ThreadDemo with main function

6 Create objects of ThreadA and ThreadB class. 

7 Call thA. start( )

8 Call thB.start( )

9 Stop


Code : 

class ThreadA extends Thread{ 

public void run() 

System.out.println("Thread A started"); 

for(int i=1;i<=6;i++) 

System.out.print("$"); 

try 

Thread.sleep(900); 

catch(Exception e) 

System.out.println(e); 

}

class ThreadB extends Thread 

public void run() 

System.out.println("Thread B started"); 

for(int i=1;i<=6;i++) 

System.out.print("*"); 

try 

Thread.sleep(900); 

catch(Exception e) 

System.out.println(e); 

class Thread_Demo 

public static void main (String args[]) 

ThreadA thA=new ThreadA(); 

ThreadB thB=new ThreadB(); 

thA.start(); 

thB.start();

}  

}


Output : 

Create multiple threads to print $*$*$*$*$*$*.


Observations and learning: 

In this program we have learned multithreading concept and use of thread class for creating different threads in program which we can use in any programming stituations 

 

Conclusion: 

We can conclude the above program is complied and executed and we have got the result if the program.In this program we have taken use of Thread class for creating different threads.

 

Question of Curiosity 

1. What is the difference between Process and Thread?

Ans : A process is a program under execution i.e an active program. A thread is a lightweight process that can be managed independently by a scheduler. Processes require more time for context switching as they are more heavy. Threads require less time for context switching as they are lighter than processes.

 

2. What are the benefits of multi-threaded programming?

Ans : 

  • Benefits of multi threading programming
  • Resource Sharing
  • Responsiveness
  • Utilization of Multiprocessor Architecture
  • Economy

 

3. What is difference between user Thread and daemon Thread?

Ans : The main difference between a user thread and a daemon thread is that your Java program will not finish execution until one of the user thread is live. On the other hand, a daemon thread doesn't get that preference, JVM will exit and close the Java program even if there is a daemon thread running in the background.

 

4. How can we create a Thread in Java?

Ans : A thread can be created by implementing the Runnable interface and overriding the run() method. Then a Thread object can be created and the start() method called. The Main thread in Java is the one that begins executing when the program starts.

 

5.What are different states in lifecycle of Thread?

Ans : The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:

  • New
  • Runnable
  • Running
  • Non-Runnable (Blocked)
  • Terminated


Comments