1. What are the Thread class constructors?
1. Thread t = new Thread();
2. Thread t = new Thread(Runnable r);
3. Thread t = new Thread(String name);
4. Thread t = new Thread(Runnable r, String name);
5. Thread t = new Thread(ThreadGroup g, String name);
6. Thread t = new Thread(ThreadGroup g, Runnable r);
7. Thread t = new Thread(ThreadGroup g, Runnable r , String name);
8. Thread t = new Thread(ThreadGroup g, Runnable r, String name, Long stackSize);
2. What are the Thread class important methods?
Method Signature
|
Description
|
String getName()
|
Retrieves the name of running thread in the current context in String format
|
void start()
|
This method will start a new thread of execution by calling run() method of Thread/runnable object.
|
void run()
|
This method is the entry point of the thread. Execution of thread starts from this method.
|
void sleep(int sleeptime)
|
This method suspend the thread for mentioned time duration in argument (sleeptime in ms)
|
void yield()
|
By invoking this method the current thread pause its execution temporarily and allow other threads to execute.
|
void join()
|
This method used to queue up a thread in execution. Once called on thread, current thread will wait till calling thread completes its execution
|
boolean isAlive()
|
This method will check if thread is alive or dead
|
3. What is Thread name
1. Every Thread in java has a by default name assigned by JVM.
2. Thread.currentThread(); returns currently executing thread.
3. t.getName(); for getting name of the thread t.
4. Default name of thread given by JVM is “Thread-0”, “Thread-1”....
public class MyThread implements Runnable {
public void run() {
System.out.println("run method"+Thread.churrentThread().getName());
}
public static void main(String[] args) {
MyThread runnable = new MyThread();
Thread thread = new Thread(runnable);
thread.start();
}
}
4. What is Thread id
- Every Thread in java has a by default id assigned by JVM and it is unique.
- Thread.currentThread(); returns currently executing thread.
- t.getId(); for getting id of the thread t.
- The thread ID is a positive long number generated when this thread was created.
- The thread ID is unique and remains unchanged during its lifetime.
- When a thread is terminated, this thread ID may be reused.
public class MyThread implements Runnable {
public void run() {
System.out.println("run method"+Thread.churrentThread().getId());
}
public static void main(String[] args) {
MyThread runnable = new MyThread();
Thread thread = new Thread(runnable);
thread.start();
}
}
No comments:
Post a Comment