Skip to main content

Posts

Showing posts with the label Threads

How to create/ schedule a Job or Thread to run at fixed intervals

Points To Remember TimerTask is a class that can run a Thread repeatedly after a specific period. You can either implement your own class also by implementing Runnable Interface. Then you may need to put your thread to sleep and then resume the thread after a desired interval. The TimerTask will do this more efficiently for you then implementing your own code. How to schedule a Thread to run after fixed interval We will be using TimerTask to schedule the Thread after every 1 sec. TimerTask class in Java implements Runnable interface and has the following methods. cancel -  to cancel the timer task when needed. scheduledExecutionTime -  gives the time when the task was last executed successfully. run -  an abstract method that needs to be implemented. MyTask.java import java.util.TimerTask; import java.util.Date; class MyTask extends TimerTask { static int count = 0; @Override public void run(){ if(count < 5){ System.out.println(" MyTask called ### count...

How to make Thread by extending Thread class

Points To Remember We can implement Runnable interface or extend Thread class to create threads. We have to override the run()  method to define the functionality of thread. We should call start()  method to start execution of a thread as a new entity, if we call run() method on a thread then it will not start a new thread. We cannot guarantee which thread will execute first even if we prioritize the threads. A thread whose run() method is executed completely reaches the dead state and cannot be invoked again. Program : Making 3 Threads using Thread Class In the following example we are making three different threads in the class Test which extends the Thread class. We just pass a parameter name to each object of Test thread to distinguish between them. class Test extends Thread{ String name; public Test(String name){ this.name = name; } public static void main(String args[]){ System.out.println("main started"); Thread t1 = new Thread(new Test("Th...

How to make threads using Runnable Interface.

Points To Remember We can make threads in java by implementing Runnable Interface . Any class that implements this interface must override its run() method. The call to start()  method internally calls the run()  method of the thread. We can not guarantee which thread would be executed first. Thread Priority only promises the cpu time and not that it will be run first .  Program : How to make threads by implementing Runnable Interface In the below example we are implementing the Runnable interface and creating two threads " t1 " and " t2 " and calling the run() method using thread .start().  We run the same program twice and we might get different outputs each time as shown below. class Test implements Runnable{ String msg; public void run(){ System.out.println("Running Thread" + msg); } public Test(String msg){ this.msg = msg; } public static void main(String args[]){ System.out.println("Stating main"); Thread t1 = new Thread(new Test(...