In this Java Video Tutorial I cover how to use Java threads.
A thread is just a block of code that is expected to execute while other blocks of code execute. That’s it. When you want to execute more than one block of code at a time you have to alert Java.
In this video I show you how to alert the interpreter. Part 1 of this series is here Java Video Tutorial. Heavily commented code follows the video.
If you like videos like this share it
The country codes I mentioned are here Country Codes
Code From the Video
LESSONSEVENTEEN.JAVA
public class LessonSeventeen{
public static void main(String[] args){
// Create a new Thread that executes the code in GetTime20
Thread getTime = new GetTime20();
// Create a new Thread created using the Runnable interface
// Execute the code in run after 10 seconds
Runnable getMail = new GetTheMail(10);
Runnable getMailAgain = new GetTheMail(20);
// Call for the code in the method run to execute
getTime.start();
new Thread(getMail).start();
new Thread(getMailAgain).start();
}
}
GETTIME20.JAVA
// By using threads you can execute multiple blocks
// of code at the same time. This program will output
// the current time and then at a specific time execute
// other code without stopping the time output
// Need this for Date and Locale classes
import java.util.*;
// Need this to format the dates
import java.text.DateFormat;
// By extending the Thread class you can run your code
// concurrently with other threads
public class GetTime20 extends Thread{
// All of the code that the thread executes must be
// in the run method, or be in a method called for
// from inside of the run method
public void run(){
// Creating fields that will contain date info
Date rightNow;
Locale currentLocale;
DateFormat timeFormatter;
DateFormat dateFormatter;
String timeOutput;
String dateOutput;
// Output the current date and time 20 times
for(int i = 1; i <= 20; i++){
// A Date object contains date and time data
rightNow = new Date();
// Locale defines time formats depending on location
currentLocale = new Locale("en", "US");
// DateFormat allows you to define dates / times using predefined
// styles DEFAULT, SHORT, MEDIUM, LONG, or FULL
// getTimeInstance only outputs time information
timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, currentLocale);
// getDateInstance only outputs time information
dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
// Convert the time and date into Strings
timeOutput = timeFormatter.format(rightNow);
dateOutput = dateFormatter.format(rightNow);
System.out.println(timeOutput);
System.out.println(dateOutput);
System.out.println();
// You must wrap the sleep method in error handling
// code to catch the InterruptedException exception
// sleep pauses thread execution for 2 seconds below
try {
Thread.sleep(2000);
}
catch(InterruptedException e)
{}
}
}
}
GETTHEMAIL.JAVA
// You can use the Runnable interface instead of
// wasting your 1 class extension.
public class GetTheMail implements Runnable {
// Stores the number of seconds before the code
// will be executed
private int startTime;
// Constructor that sets the wait time for each
// new Thread
public GetTheMail(int startTime){
this.startTime = startTime;
}
// All of the code that the thread executes must be
// in the run method, or be in a method called for
// from inside of the run method
public void run(){
try
{
// Don't execute until 10 seconds has passed if
// startTime equals 10
Thread.sleep(startTime * 1000);
}
catch(InterruptedException e)
{}
System.out.println("Checking for Mail");
}
}
Hey nice video, but i have a question, although localization is used to change the entire webpage into country specific language, but what i have to do, if i want to convert the entire webpage into state/city specific language (ie. hindi, marathi), how can i do this???
I’ll get into language specific translation when I start making JavaServer pages. This is basically for stand alone applications instead of web applications. I’ll do my best to explain that topic, but I’m not very well versed in other speaking languages
thank you for response, i also want to know for web applications, if you know anything about this, please let me know, where should i look for this??
Thank you..
I will cover developing web applications with Java very soon.
okie, thank you very much…
Hey! I like your java tutorials. I think your regex tutorial is best.
I want to know which IDE you use because I use notepad++ & in notepad++ console window is not at the right side.
And, which screen recording software you use for making these tuts because it’s just awesome!!!.
I use Eclipse because it is free and looks exactly the same on every OS. A definite plus if you are making video tutorials for the world.
I record my screencasts with Quicktime Player and edit them with iMovie. I used to think that Quicktime Player was the best, but since the last update it is kind of broken. I plan on purchasing a new screencasting program but haven’t decided which one yet.
Thanks for stopping by
Thanks for reply!!
If possible then make video tutorial about java.io(Input/Output) or about file. I want to completely understand which class used for what because there is streamreader & bufferedreader & char stream and Scanner etc.It is a bit confusing!!!!
I’ll be covering that later in the current tutorial. Don’t worry I’ll get there soon