Knowee
Questions
Features
Study Tools

what is thread in java

Question

what is thread in java

🧐 Not the exact question you are looking for?Go ask a question

Solution

A thread in Java is a lightweight sub-process, the smallest unit of processing. It is a separate path of execution. Threads are independent, if there occurs exception in one thread, it doesn't affect other threads. It shares a common memory area. They are used to perform complicated tasks in the background without interrupting the main program.

Here are the steps to create a thread in Java:

  1. By extending Thread class: Java Thread class can be extended by a class to create a thread. After extending the Thread class, a class would inherit all the methods of Thread class and you can create a thread by instantiating that class.

  2. By implementing Runnable Interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

  3. By Callable and Future: Java 5 introduced java.util.concurrent.Callable interface in concurrency package that is similar to Runnable interface but it can return any Object and able to throw Exception.

  4. Using Anonymous class: You can also create a thread by using an anonymous class that either extends the Thread class or implements the Runnable interface.

This problem has been solved

Similar Questions

Explain Multithreading in Java

correct answerWhat is the name of the thread in the following Java Program?class CT{  public static void main(String args[])  {    Thread s = Thread.currentThread();    System.out.println(s);       }}

Select the correct answerIn the following java program, what is the NAME of the thread?class Nptel extends Thread{ public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println(t); }}OptionsthreadsystemNone of thesemain

Which method is used to create a new thread for long-running tasks in Java?Question 3Answera.newThread()b.startThread()c.runThread()d.invokeLater()

What will be the output of the following Java code?class multithreaded_programing { public static void main(String args[]) {Thread t = Thread.currentThread();System.out.println(t); } } Thread[5,main]Thread[main,5]Thread[main,0]Thread[main,5,main]

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.