These are very common questions that a beginner Java developer may ask while learning Multi-threading in Java programming.
What are the ways in which one can create a Java Thread ?Answer : There are two ways. 1 : Using Thread class, 2 : Using Runnable Interface.
What is the difference between Thread and Runnable ?Answer : Thread is a class from java.lang.Thread package , where as Runnable is an interface from java.lang.Runtime package.
Should use Thread Class or Runnable interface and Why?Answer : It depends, but as much as possible we must make use of Runnable interface, We must try to extend a class only when we want to override some behavior. When we extend a class, the two classes are very tightly coupled to each other.
Example: Thread Class: hreadClassExample.javapackage com.code2care.examples;
public class ThreadClassExample extends Thread {
Thread thread;
ThreadClassExample() {
thread = new Thread(this);
System.out.println(thread + " : Started!");
thread.start();
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread : " + thread.getName() + " " + i);
}
}
}
File: ThreadClient.java
package com.code2care.examples;
public class ThreadClient {
public static void main(String[] args) {
new ThreadClassExample();
}
}
Example: Runnable Interface: RunnableExample.java
package com.code2care.examples;
public class RunnableExample implements Runnable {
Thread thread;
RunnableExample() {
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
for (int i = 1; i <= 10; i++) {
System.out.println("Thread : " + thread.getName() + " " + i);
}
}
}
ThreadClient.java
package com.code2care.examples;
public class ThreadClient {
public static void main(String[] args) {
new ThreadClassExample();
}
}
More Posts related to Java,
- Convert Java Map Collection Object to JSON String using Jackson
- Java Stream flatmap() Examples
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years
- How to run Java Unit Test cases with Apache Maven?
- How to check if Java main thread is alive
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Parsing CSV file using Java code example (Comma Separated File)
- Unhandled exception type InterruptedException : Java Threads
- Native getClass() method from java.lang.Object Class Explained with examples.
- Java Jackson ObjectMapper Class with Examples
- Java 8 Streams map() with examples
- Java 8 - Convert List to Map Examples
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Java Stream with Multiple Filters Example
- How to Clear StringJoiner in Java 8
- Spring 5 IoC Example with application Context XML (ClassPathXmlApplicationContext) and Gradle.
- How to get end of line (EOL) or new line character \r \n in Java
- Spring Boot CRUD Examples using JDBCTemplate
- Delete a File in Java with Examples
- Implementing Insertion Sort Algorithm in Java Program
- Java JDBC Batch Update Example with PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to fix Java HTTP java.net.UnknownHostException
- Java 8 Display time in 12 hour AM PM format
More Posts:
- How to use SCP Command to Copy Directory - Linux
- How to enable missing SharePoint Site Assets, Site Pages library App - SharePoint
- How to Undo-Revert Sent Email in Google Gmail - Google
- AutoSave button not working on Office on Mac (Word, Excel or Powerpoint) - MacOS
- List of Java JDK Versions till Year 2023 with LTS Support Included - Java
- Display (Show) bookmarks bar Safari - HowTos
- [fix] openssl No such file or directory error C++ - Ubuntu
- How to run .bat file on Mac - MacOS