Java examples on java.lang
- How do I reverse a string?
- How do I add leading zeros to a number?
- How do I split a string?
- How do I get file separator symbol?
- How do I terminate a Java application?
- How do I get operating system temporary directory / folder?
- What is Autoboxing?
- How do I remove trailing white space from a string?
- How do I calculate process elapsed time?
- How do I copy char array to string?
- How do I convert string to char array?
- How do I get package name of a class?
- How do I check if a character representing a number?
- How do I check if a character representing an alphabet?
- How do I get the username of operating system active user?
- How do I remove substring from StringBuilder?
- How do I add a delay in my program?
- How do I get the name of current executed method?
- How do I decode string to integer?
- How do I get super class of an object?
- How do I get Java Home directory?
- How do I convert string to an integer or number?
- How do I use for-each in Java?
- How do I check string for equality?
- How do I get Environment Variables?
- How do I get user home directory name?
- How do I replace characters in string?
- How do I read system property as an integer?
- How do I check if a string contains a specific word?
- How do I create random number?
- How do I reverse a string by word?
- How do I convert other primitive data type into string?
- How do I create a console progress bar?
- How do I get path / classpath separator?
- How do I execute other applications from Java?
- How do I convert decimal to octal?
- How do I get a part or a substring of a string?
- How do I check if a string is a valid number?
- How do I know if a character is lowercase?
- How do I get operating system name and version?
- How do I determine whether a string is a palindrome?
- How do I get the minimum and maximum value of a primitive data types?
- How do I know the length of a string?
- How do I convert varargs to an array?
- How do I know if a character is uppercase?
- How do I get Java Runtime Environment (JRE) version?
- How do I check a string ends with a specified word?
- How do I convert decimal to binary?
- How do I convert decimal to hexadecimal?
- How do I check a string starts with a specified word?
- How do I check if a thread is alive?
- How do I know the memory size in virtual machine?
- How do I copy some items of an array into another array?
- How do I create a thread by implementing Runnable interface?
- How do I know a class of an object?
- How do I use join method to wait for threads to finish?
- How do I set the priority of a thread?
- Can I create a boolean variable from string?
- How do I remove leading white space from a string?
- How do I set and get the name of a thread?
- How do I get interfaces implemented by a class?
- How do I count the number of occurrences of a char in a String?
- How do I insert a string in the StringBuilder?
- How do I convert primitive boolean type into Boolean object?
- How do I create a deamon thread?
- How do I create a thread by extending Thread class?
- How do I create a multithread program?
- How do I create a method that accept varargs in Java?
- How do I clear system property?
- How do I validate email address using regular expression?
- How do I pause the current thread?
- How do I start a thread execution?
- How do I calculate process execution time in higher resolution?
- How do I get the currently executing thread?
- How do I remove leading and trailing white space from string?
- How do I use the sleep method of the Thread class?
- How do I check if a thread is a daemon thread?
- How do I get the name of a class?
- How do I know if a class object is an interface or a class?
- How do I create a thread synchronized block?
- How do I check whether a thread group has been destroyed?
- How do I use multi-catch statement?
- How do I count active thread in a thread group?
- How do I create array of unique values from another array?
- How do I remove some characters from a StringBuffer?
- How do I destroy a thread group?
- How do I execute external command and obtain the result?
- How do I get the path from where a class is loaded?
- How do I get the state of a thread?
- How do I get number of active thread group?
- How do I get thread group of a thread?
- How do I get number of active thread in current thread?
- How do I get char value of a string at a specified position?
- How do I append and replace content of a StringBuffer?
- How do I insert a string to a StringBuffer?
- How do I split a string with multiple spaces?
- How to split a string by a number of characters?
How do I create a multithread program?
A thread is a unit of program execution that runs on their own thread. Multithreading is a core capabilities of the Java language which was there when the language was created. This multithreading capabilities can be said like running each program on their on CPU, although the machine only has a single CPU installed.
To create a Thread program in Java we can extends the java.lang.Thread class and override the run() method. But as we know that Java class can only extends from a single class, extending Thread class makes our class cannot be inherited from another class. To solve this problem an interface was introduced, the java.lang.Runnable.
Let see the simplified demo class below. In the program below we'll have three separated thread executions, the main thread, thread-1 and thread-2.
package org.kodejava.example.lang;
public class ThreadDemo extends Thread {
public static void main(String[] args) {
//
// Creates an instance of this class.
//
ThreadDemo thread1 = new ThreadDemo();
//
// Creates a runnable object.
//
Thread thread2 = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 5; i++) {
sayHello();
}
}
});
//
// Set thread priority, the normal priority of a thread is 5.
//
thread1.setPriority(4);
thread2.setPriority(6);
//
// Start the execution of thread1 and thread2
//
thread1.start();
thread2.start();
for (int i = 0; i < 5; i++) {
sayHello();
}
}
public void run() {
for (int i = 0; i < 5; i++) {
sayHello();
}
}
/**
* The synchronized modifier ensure that two threads cannot execute the block
* at the same time.
*/
private static synchronized void sayHello() {
for (int i = 0; i < 1000; i++) {
System.out.println("Thread [" + Thread.currentThread().getName() + "] ==> Hi...");
}
try {
//
// Causes the currently executing thread to sleep for a random
// milliseconds
//
Thread.sleep((long) Math.random() * 1000 + 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//
// Causes the currently executing thread object to temporarily pause
// and allow other threads to execute.
//
Thread.yield();
}
}