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();
}
}
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
- Display Seconds in Digital Clock on Mac Menu Bar [macOS] - MacOS
- Installing Google Cloud macOS SDK - Google
- How to delete all text after a character or string in Notepad++ - NotepadPlusPlus
- bash: netstat: command not found - Bash
- Hide Navigation Bar from Android Screen Activity - Android
- Java Read and Write Properties file with Examples - Java
- Error:The SDK Build Tools revision (XX.X.X) is too low for project. Minimum required is XX.X.X - Android
- How to find the installed version of Microsoft Teams - Teams