answersLogoWhite

0

Which among is thread class a sleep b isLive c isAlive?

Updated: 8/18/2019
User Avatar

Wiki User

12y ago

Best Answer

sleep ()..

Runnable r1 = new Runnable() { public void run() { try { while (true) { System.out.println("Hello, world!"); Thread.sleep(1000L); }

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Which among is thread class a sleep b isLive c isAlive?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

If Runnable interface is better than Thread class than why you are using Thread class What is the need for Thread class?

Though implementing Runnable interface is better approach than inheriting from Thread class but there are certain methods available in Thread class like join,sleep or yield which does not available in Runnable interface and programmer can not use them unless he has object of Thread class. This is why we should have Thread class Object. Rupesh Raghani


What are the two ways of spawning a thread in Java?

You can create a Thread in Java by using two ways. 1. Extending the Thread class public class Test extends Thread { ..... } 2. Implementing the Runnable Interface public class Test implements Runnable { ... }


What is inline thread in java?

You can start a thread "inline" without implementing Runnable or extending Thread class( new Thread() { public void run(){// do something} } ).start();


Can you synchronize a class?

No. If someone refers to a "synchronized class," they are generally talking about a class in which all methods are thread-safe.


How does thread synchronization occur in a monitor?

by using synchronized class


How are java threads created?

Thread can be created in two ways either by extending Thread or implementing runnable interface Examples: public class A extends Thread { } public class A implements Runnable { } By using both the methods we can create a Java Thread. The disadvantage of extending the Thread class is that, you cannot extend the features of any other class. If we extend the Thread class we cannot extend any other class whose features we may require in this class. So it is usually advisable to implement the Runnable Interface so that we can extend the required features and at the same time use the Thread functionality.


What is upavita?

in Hinduism, the Upavita is a Sacred Thread worn by the elite class


Why do you need runnable interface?

A Runnable Interface is one that is used to create a Java Thread... A Thread can be created in two ways and using the Runnable Interface is one of them. Example: public class Test implements Runnable { public void run(){ .... } } The Runnable interface would have an abstract instance of the method run() which needs to be implemented in the class which wants to create a Thread.


Multithreading in java?

I am assuming that you want to know how to multithread in Java. 1) Write a class that implements Runnable. Put just the method run() in it. 2) Inside the run() method, put the code that you want your thread to run. 3) Instantiate the class (example: Runnable runnable = new MyRunnable();) 4) Make a new Thread (example: Thread thread = new Thread(runnable, <the name of your thread(optional)>); 5) Start the thread (example: thread.start();) 6) That's it! Your thread is now running. PS. Check the Java API for more information. Did that answer your question?


What a thread executes?

A thread can do anything a regular class does. However, the idea of having multiple threads is so that the computer can do several things simultaneously.


How do you make thread in Java?

Creating a ThreadA thread in Java begins as an instance of java.lang.Thread. You'll find methods in the Thread class for managing threads including creating, starting, and pausing them. They are:start()yield()sleep()run()All the awesome action happens in the run() method. Think of the code you want to execute in a separate thread as the job to do. Lets say, you have some work that needs to be done, in the background while other things are happening in the program, so what you really want is that work to be executed in its own thread. All that code you want executed in a separate thread goes into the run() method.Ex:public void run() {// your job code goes here}The run() method will call other methods, of course, but the thread of execution-the new call stack-always begins by invoking run(). So where does the run() method go? In one of the two classes you can use to define your thread job.You can define and instantiate a thread in one of two ways:• Extend the java.lang.Thread class.• Implement the Runnable interface.You need to know about both for the exam, although I would personally recommend that you implement Runnable than extend Thread. Extending the Thread class is the easiest, but it's usually not a good OO practice.Now, you may ask me "Why? Why should I choose the Runnable over Thread?"Well, if you thought of the why before reading the preceding line, give yourself a pat on the back. Well done!Because, if you extend the Thread class what would you do if you have to extend another concrete parent class that has vital/important behavior that you need in your class? Get the point? Java does not support direct multiple inheritance and hence, once you extend the Thread class, you are done, for good. You cannot extend any other class. That is why I recommended the Runnable interface. Even if you implement the interface, you are free to extend any class you want. Convenient, right?Defining a ThreadTo define a thread, you need a place to put your run() method, and as we just discussed, you can do that by extending the Thread class or by implementing the Runnable interface. We'll look at both in this section.Extending java.lang.ThreadThe simplest way to define code to run in a separate thread is to• Extend the java.lang.Thread class.• Override the run() method.It looks like this:class MyFirstThread extends Thread {public void run() {System.out.println("Important job running in MyFirstThread");}}The limitation with this approach is that if you extend Thread, you can't extend anything else. And it's not as if you really need that inherited Thread class behavior, because in order to use a thread you'll need to instantiate one anyway.Keep in mind that you're free to overload the run() method in your Thread subclass:class MyFirstThread extends Thread {public void run() {System.out.println("Important job running in MyFirstThread");}public void run(String s) {System.out.println("String running is " + s);}}Note: The overloaded run(String s) method will be ignored by the Thread class unless you call it yourself. The Thread class expects a run() method with no arguments, and it will execute this method for you in a separate call stack after the thread has been started. With a run(String s) method, the Thread class won't call the method for you, and even if you call the method directly yourself, execution won't happen in a new thread of execution with a separate call stack. It will just happen in the same call stack as the code that you made the call from, just like any other normal method call.Implementing java.lang.RunnableImplementing the Runnable interface gives you a way to extend any class you like, but still define behavior that will be run by a separate thread. It looks like this:class MyFirstRunnableClass implements Runnable {public void run() {System.out.println("Imp job running in MyFirstRunnableClass");}}Regardless of which mechanism you choose, you've now got yourself some code that can be run by a thread of execution. So now let's take a look at instantiating your thread-capable class, and then we'll figure out how to actually get the thing running.Instantiating a ThreadRemember, every thread of execution begins as an instance of class Thread. Regardless of whether your run() method is in a Thread subclass or a Runnable implementation class, you still need a Thread object to do the work.If you extended the Thread class, instantiation is dead simple:MyFirstThread t = new MyFirstThread()If you implement Runnable, instantiation is only slightly less simple. To have code run by a separate thread, you still need a Thread instance. But rather than combining both the thread and the run() method into one class, you've split it into two classes-the Thread class for the thread-specific code and your Runnable implementation class for your job-that-should-be-run-by-a-thread code.First, you instantiate your Runnable class:MyFirstRunnableClass r = new MyFirstRunnableClass();Next, you get yourself an instance of java.lang. Thread, and you give it your job!Thread t = new Thread(r); // Pass your Runnable to the ThreadIf you create a thread using the no-arg constructor, the thread will call its own run() method when it's time to start working. That's exactly what you want when you extend Thread, but when you use Runnable, you need to tell the new thread to use your run() method rather than its own. The Runnable you pass to the Thread constructor is called the target or the target Runnable.You can pass a single Runnable instance to multiple Thread objects, so that the same Runnable becomes the target of multiple threads, as follows:public class TestMyThreads {public static void main (String [] args) {MyFirstRunnableClass r = new MyFirstRunnableClass();Thread aaa = new Thread(r);Thread bbb = new Thread(r);Thread ccc = new Thread(r);}}Giving the same target to multiple threads means that several threads of execution will be running the very same job and that same job will be done multiple times.


What are the different ways of implementing thread in java?

You can define and instantiate a thread in one of two ways: • Extend the java.lang.Thread class. • Implement the Runnable interface. class MyFirstThread extends Thread { public void run() { System.out.println("Important job running in MyFirstThread"); } } or class MyFirstRunnableClass implements Runnable { public void run() { System.out.println("Imp job running in MyFirstRunnableClass"); } }