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,
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Difference between using Scanner Class and String args for user input in Java
- Maven Unsupported major.minor version 51.0
- Java: TimeZone List with GMT/UTC Offset
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Java Split String by Spaces
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- hibernate.cfg.xml Configuration and Mapping xml Example
More Posts:
- reCaptcha Verification expired. Check the checkbox again - Html
- Android Studio Native typeface cannot be made error - Android
- MySQL Error :1007 SQLSTATE: HY000 (ER_DB_CREATE_EXISTS) Message: Can't create database '%s'; database exists - MySQL
- Mac OS X Error: Could not create the Java Virtual Machine - Mac-OS-X
- Android [SDK Manager] The system cannot find the path specified - Android-Studio
- How to stop disable Facebook video autoplay during scroll - Facebook
- NewApi error : Finds API accesses to APIs that are not supported in all targeted API versions - Android
- JSON Text to JavaScript Object using eval() Example: JSON Tutorial - Json-Tutorial
- Failed to load the JNI shared library jvm.dll - Eclipse
- PHP Script to Upload Images to Server - PHP
- Get Device Screen Width and Height using javaScript - JavaScript
- How to get SharePoint List Item URL using PowerShell - SharePoint
- Your Android SDK is missing, out of date or corrupted SDK Problem - Android-Studio
- Send Email with attachment using SharePoint PowerShell - SharePoint
- How to add Newline to text in Android TextView - Android