Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

java multithreading

is multithreading performed automatically or programmer has to implement
0
devender
Asked:
devender
  • 7
  • 5
  • 3
  • +3
1 Solution
 
Sasha_MapaCommented:
Well, of course the programmer has to implement multithreading, otherwise I don't understand the question.
To start a new thread you must extend the Thread class, override the run() method to do something useful, instantiate your class and call start() on the object.
An alternative way to start a new thread is to have some class implement the Runnable interface by implementing the run() method, instantiate the Thread class with an instance of your Runnable class and call start() on the Thread object, here is an example:

public class Ticker implements Runnable{

  private int time = 0;
  private Thread myThread;

  public void startTicking(){
    myThread = new Thread(this);
    myThread.start();
  }

  public void run(){
    while (myThread!=null){
      try{
        Thread.sleep(1000); // sleep for one sec.
      } catch (InterruptedException e){
           return;
         }
       System.out.println("The current time is: "+(++time)+" seconds.");
    }
  }

  public void stopTicking(){
    myThread = null;
  }

}

If you have any further questions, ask.
0
 
rishabh_JavaCommented:
basically Multithreading is Operating System dependent & ofcourse its synchronisation of the Program and the
O.S..U write a program which will create the threads out of the main thread(main Application)by means of either Thread class  or Runnable inetrface.
U design u'r program to create the threads as per the tasks u have in hand
and u send it for execution.Then U'r O.S. checks and creates the Primary thread(main Application) and then secondary threads as per the program design(if its 32 bit O.S. supp Multithreaded Application).

for all the Thread/Process related issues it has to rely on the O.S. approach.U'r program may / may not flow as U designed.

If U do not agree to this , Please put a detailed comments.
Thank U.

Rishabh
0
 
Sasha_MapaCommented:
I think I will post detailed comments:
First, the actual implementation of the threads in the OS your program runs in is absolutely transparent to you, it is the JVMs responsibility to manage threads and if the OS can't, the JVM must implement it by itself. Multithreaded Java programs can even successfully run on OSes that don't allow multithreading... The JVM creates the environment in which the program runs so Java programmers will not usually care what platform is used.
Second, a well designed program will use multithreading in a way that it will always flow in the designed manner, not as you claim that it may/may not flow as designed.
This was not (I believe) the question that was asked, though it was not very clear in the first place.
I do not feel it is appropriate that you posted an answer here because (A) the question is not clear enough to know what exactly is wanted and (B) your answer is not only incorrect, but I'm not sure how it relates to the question (but then again, the question is not very clear). rishabh, please withdraw your answer...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
rishabh_JavaCommented:
I am not very clear about JVM's approch for Multithreading but as JVM uses the
bytecodes and inetrprets them as per the underlying O.S. implementation,I doubt ,multithreaded prog can run on
O.S. which may not support  it all.
still,I'm withdrawing my answer,please
give me the reference for your comments
so as to confirm the things.
Thanks
Rishabh
0
 
Sasha_MapaCommented:
Like I said, the JVM is responsible to create a uniform environment on all platforms for the Java programs to run in. Did you wonder for example how an int is always 4 bytes in Java and a long is 8 bytes even on 16bit platforms? The JVM makes sure that even if on the platform it runs on an int is just 2 bytes, it reserves enough space for  the "Java" int and manages all operations on it. Of course a windows implementation of the JVM will use the resources already available to it from windows instead of reimplementing, but a JVM on a platform without multitasking will have no choice but to implement it itself. Otherwise, it is not a JVM. If the case were not that way, then Java would not have been platform independent, but it IS.
Reference to my comment? Read the Java Virtual Machine specification at http://java.sun.com/docs/books/vmspec/index.html 
0
 
rishabh_JavaCommented:
Thanks devender for upgrading rather
putting me on to right track.
0
 
rishabh_JavaCommented:
Thanks devender for upgrading rather
putting me on to right track.
0
 
rishabh_JavaCommented:
Thanks devender for upgrading rather
putting me on to right track.
0
 
vinesh_indiaCommented:
thanq sasha and rishab
but how is multithreading implemented in servlets
0
 
Sasha_MapaCommented:
In the exact same way. Servlets are written in Java, and the multithreading stuff is a part of the Java language, it's not a part of any libraries such as java.awt or java.applet... It is available to ALL applications written in Java, including applets, servlets and standalone applications.
0
 
vinesh_indiaCommented:
how is an thread created for each and
every request for a servlet
0
 
Sasha_MapaCommented:
Umm, sorry that's already a servlet specific question, and I am not familiar with servlets. If it is created then probably the servlet package asks the server to create another thread and runs your code in it. In any case, you can create threads by yourself and run in them whatever you want in the manner I showed above...
0
 
vinesh_indiaCommented:
thanq sahapa
0
 
Sasha_MapaCommented:
Excuse me?? An answer?? Please withdraw it...
0
 
darinwCommented:
This is a very strange thread.

devender do you have two accounts? Why would vinesh_india post a 'thanks' to anyone?

I am rejecting the proposed answer and I want devender/vinesh_india to email me concerning the duplicate account - darin@experts-exchange.com. One account will have to go.

If you were not aware of it, you are allowed only ONE account at Experts Exchange per the Customer Agreement http://www.experts-exchange.com/info/usrcntr.html

darinw
Customer Service
0
 
dotandCommented:
Hi,

For servlets the servlet runner (or web server) will start a thread for each client attempting to connect.

You should make your servlets multi-thread safe if this is what you desire. If you want to have serialized access to your servlets than you should implememnt the SingleThreadModel interface and the servlet runner will take care of this for you.

As for running several threads while the servlet is executing it is possible just like any other multi threading program.

HTH,
Dotan
0
 
darinwCommented:
Sasha_Mapa repost your comment as an Answer please.

darinw
Customer Service
0
 
darinwCommented:
dotand the poster had already indicated that Sasha_Mapa had given an acceptable answer so I am rejecting your proposed answer.

darinw
Customer Service
0
 
Sasha_MapaCommented:
Well, of course the programmer has to implement multithreading, otherwise I don't understand the question.
To start a new thread you must extend the Thread class, override the run() method to do something useful, instantiate your class and call start() on the object.
An alternative way to start a new thread is to have some class implement the Runnable interface by implementing the run() method, instantiate the Thread class with an instance of your Runnable class and call start() on the Thread object, here is an example:

public class Ticker implements Runnable{

   private int time = 0;
   private Thread myThread;

   public void startTicking(){
      myThread = new Thread(this);
      myThread.start();
   }

   public void run(){
      while (myThread!=null){
        try{
           Thread.sleep(1000); // sleep for one sec.
        } catch (InterruptedException e){
              return;
           }
        System.out.println("The current time is: "+(++time)+" seconds.");
     }
   }

   public void stopTicking(){
       myThread = null;
   }

}
0
 
devenderAuthor Commented:
i agree with the answer
thank u
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now