How many ways can a thread be used?
Answers were Sorted based on User's Feedback
Answer / harsha
The thread implemented by using runnable interface is more
advantageous. The other way of using a thread is by
inheriting from the Thread class.
| Is This Answer Correct ? | 13 Yes | 1 No |
Answer / muruganantham
Two ways we can using in Thread. One is Thread class and
another one is Runnable interface. If we are using extends
Thread class means you must override the run() method. We
can create a thread object after that we used start method.
Its only one waiting (wait())thread can be executed
Ex:
Thread t=new Thread();
t.start();
If we are using implement Runnable Interface means you need
not override the run() method. We can passing the parameter
is thread . Its more than one waiting (wait())thread can be
executed.
Hi
This is Muruga. Sorry for gramatical mistake bze that my
level. If any feed back pl reply to my mail id
sunjavamuruga@gmail.com
| Is This Answer Correct ? | 8 Yes | 1 No |
Answer / subha narayan mohapatra
thread can be used in two ways-: extending the Thread class
and implementing Runnable interface.
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / vikneswaran
two way to create a thread one is extends Thread classs
another is Runable interface suppose one class extends
another class we cant create for theard class so go for
interface.
1.Thread obj=new Thread();
2.Thread obj=new Thread(class obj);(Runable interface)
Example program:
//creating the thread by extends thread class
public class ThreadSample1 extends Thread
{
boolean runthread = true;
//override the run method
public void run ()
{
while (runthread)//thread will run untill runthread
flag is true
{
System.out.println("Inside thread, name is " +
getName());
try
{ //it will be wait state for 100ms, after that
it will come to run state
//to call sleep method, we should handle the
exception using try catch block
System.out.println("Inside thread, going to
sleep for 100ms");
Thread.sleep(100);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main (String args[])
{
//Object creation
ThreadSample1 clsObj = new ThreadSample1();
//set the priority of the thread, default is
NORM_PRIORITY
clsObj.setPriority(Thread.MAX_PRIORITY); //priority
number is 10
//clsObj.setPriority
(Thread.NORM_PRIORITY); //priority number is 5
//clsObj.setPriority(Thread.MIN_PRIORITY);//priority
number is 1
//clsObj.setPriority(Thread.NORM_PRIORITY +
1);//priority number is 6
//set the thread name. Default thread name is Thread-
1
clsObj.setName("FirstThread");
//thread id will be assigned randomly during the
runtime
System.out.println("Thread Id is " + clsObj.getId());
//get the thread name
System.out.println("Thread Name is " + clsObj.getName
());
//get the thread priority
System.out.println("Thread Priority is " +
clsObj.getPriority());
//get the thread state
System.out.println("Before start : Thread State is "
+ clsObj.getState());
//start the thread
clsObj.start();
/* we can create the thread as follows
ThreadSample1 clsObj1 = new ThreadSample1();
clsObj1.setName("TestThread");
clsObj1.setPriority(5);
Thread threadObj = new Thread(clsObj1);
threadObj.start();
Thread threadObj1 = new Thread(new ThreadSample1());
threadObj1.start();
*/
Thread th2 = new Thread(new ThreadSample1());
th2.start();
System.out.println("After start : Thread State is "
+ clsObj.getState());
//to suspend the thread, it will just pause the
execution and keep its in current state
clsObj.suspend();
//get the thread state
System.out.println("After Suspend : Thread State
is " + clsObj.getState());
//resume the thread from the suspend
clsObj.resume();
System.out.println("Thread alive state is " +
clsObj.isAlive());
//to stop the thread, after completion of run, it
will go to dead state
clsObj.runthread = false;
//force the thread to stop.
//clsObj.stop();
System.out.println("After stop : Thread State is " +
clsObj.getState());
}
}
Runable interface program:
//creating the thread by implements runnable interface
public class ThreadSample2 implements Runnable
{
boolean runthread = true;
//override the run method
public void run ()
{
while (runthread)
{
System.out.println("Inside thread");
try
{ //it will be wait state for 1000ms, after that
it will come to run state
//to call sleep method, we should handle the
exception using try catch block
Thread.sleep(1000);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public static void main (String args[])
{
//Object creation
ThreadSample2 clsObj = new ThreadSample2();
//since its implements runbable interface,
//we cannt do the follwoing for this way of thread
creation
//set the priority of the thread, default is
NORM_PRIORITY
//clsObj.setPriority(Thread.MAX_PRIORITY); //priority
number is 10
//set the thread name. Default thread name is Thread-1
//clsObj.setName("FirstThread");
//thread id will be assigned randomly during the
runtime
//System.out.println("Thread Id is " + clsObj.getId
());
//get the thread name
//System.out.println("Thread Name is " +
clsObj.getName());
//get the thread priority
//System.out.println("Thread Priority is " +
clsObj.getPriority());
//get the thread state
//System.out.println("Before start : Thread State
is " + clsObj.getState());
//start the thread
//clsObj.start();
//but we can do through this way
ThreadSample2 clsObj1 = new ThreadSample2();
Thread threadObj = new Thread(clsObj1);
/*
Thread th2 = new Thread(new ThreadSample1());
th2.start();
*/
threadObj.setName("runnable");
threadObj.setPriority
(Thread.MAX_PRIORITY); //priority number is 10
//thread id will be assigned randomly during the
runtime
System.out.println("Thread Id is " + threadObj.getId
());
//get the thread name
System.out.println("Thread Name is " +
threadObj.getName());
//get the thread priority
System.out.println("Thread Priority is " +
threadObj.getPriority());
//get the thread state
System.out.println("Before start : Thread State is "
+ threadObj.getState());
threadObj.start();
System.out.println("After start : Thread name is " +
threadObj.getName());
//to suspend the thread, it will just pause the
execution and keep its in current state
threadObj.suspend();
//get the thread state
System.out.println("After Suspend : Thread State is "
+ threadObj.getState());
//resume the thread from the suspend
threadObj.resume();
System.out.println("Thread alive state is " +
threadObj.isAlive());
//to stop the thread, after completion of run, it
will go to dead state
clsObj.runthread = false;
//force the thread to stop.
threadObj.stop();
System.out.println("After stop : Thread State is " +
threadObj.getState());
}
}
Synchronized, multithread and threadgruop
------------------------------------------------------------
--------------------
/**
* @author Vikneshwaran
* @version 1.0
* #################### Template #####################
* class thread_class_name extends Thread
* {
* data_type base_var_name = var_value;
*
* public void run()
* {
* //do something
* }
*
* public synchronized void method_name()
* {
* //do something
* }
*
* public static void main (String args[])
* {
* //Multi thread
* thread_class_name obj1 = new thread_class_name();
* thread_class_name obj2 = new thread_class_name();
* thread_class_name obj3 = new thread_class_name();
*
* obj1.start();
* obj2.start();
* obj3.start();
*
* //Thread group
* thread_class_name obj = new thread_class_name();
* ThreadGroup thdGrp = ThreadGroup("groupname");
*
* Thread threadOb1 = new Thread(threadGrp, clsObj3);
* Thread threadOb2 = new Thread(threadGrp, clsObj3);
* Thread threadOb3 = new Thread(threadGrp, clsObj3);
*
* threadGrp.setMaxPriority(Thread.NORM_PRIORITY);
* threadGrp.stop();
* }
* }
| Is This Answer Correct ? | 0 Yes | 3 No |
Can we have more than one action servlet?
How are the elements of a cardlayout organized?
Hello, I'm java developer. My skill is programming with java,jsp, struts, struts2+spring, hibernate, eclipse, tomcat, mysql,Oracle,Middlegen and other open source technology. I'm interest work any web base project that use my ability above. The thing is that I have never got any chance to work in this field though i am very much comfort with my skills. So if any body want me then i will happily join you. Also I have also some of my friends if you. If you interest to outsource any project to me then also you are welcome. you can reach me by email at me4bangalore@yahoo.in
What is the difference between Super and This Keyword?
Name the class that is used to bind the server object with RMI Registry?
how to make a index.jsp for running the site in internet and find an error for connection with weblogic server and java that give an error invalid object name.and how to maintain session.
Which characters may be used as the second character of an identifier, but not as the first character of an identifier?
Are enterprise beans allowed to use thread.sleep()?
What is source and listener?
How to pass parameters in RMI?
which is the better collection class if the requirement is only displaying the data
What is the difference between the session.update() method and the session.lock() method?