100 Days of Code #Day 7

Lakshmanan Subbiah
1 min readFeb 25, 2021

--

Today is special. It completes the first week of my 100 days of code. So I want to do something different today. Instead of solving a stereotype question from leetcode or hackerrank I plan today to move towards a domain which I am not much used too.

Threads Scheduling

Today we will start with simple schedule tasks programs which can be used to schedule an one time task.

We use java.util.Timer and java.util.TimerTask which can be collectively referred as Java Timer framework makes easy for us to schedule simple tasks.

Timer and Timer task allows us to create a thread that runs in the background, waiting for a specific time. When the time arrives the task linked to the thread is executed. Timer and TimerTask work together. Timer is the class we use to schedule the task execution. The task being scheduled must be an instance of TimerTask. Thus to schedule a task we have to create a TimerTask first and then schedule it for execution using an instance of Timer.

Once a Timer has been created, we can schedule a task by calling schedule( ) on the Timer that has been created

void schedule(TimerTask Task, long wait, long repeat);

I have created a sample Timed program to display some content over a specified period of time.

The executable part of above code can be found here :

#100DaysOfCode #Day7 #Threads

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response