[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

java multithreading

Posted on 2000-05-07
20
Medium Priority
?
422 Views
Last Modified: 2013-11-23
is multithreading performed automatically or programmer has to implement
0
Comment
Question by:devender
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 3
  • +3
20 Comments
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2785543
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
 

Expert Comment

by:rishabh_Java
ID: 2787453
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2787680
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:rishabh_Java
ID: 2788281
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
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2788720
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
 

Expert Comment

by:rishabh_Java
ID: 2791161
Thanks devender for upgrading rather
putting me on to right track.
0
 

Expert Comment

by:rishabh_Java
ID: 2791162
Thanks devender for upgrading rather
putting me on to right track.
0
 

Expert Comment

by:rishabh_Java
ID: 2791163
Thanks devender for upgrading rather
putting me on to right track.
0
 

Expert Comment

by:vinesh_india
ID: 2791652
thanq sasha and rishab
but how is multithreading implemented in servlets
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2791911
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
 

Expert Comment

by:vinesh_india
ID: 2792865
how is an thread created for each and
every request for a servlet
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2793042
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
 

Expert Comment

by:vinesh_india
ID: 2793077
thanq sahapa
0
 
LVL 7

Expert Comment

by:Sasha_Mapa
ID: 2793115
Excuse me?? An answer?? Please withdraw it...
0
 
LVL 3

Expert Comment

by:darinw
ID: 2808394
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
 
LVL 1

Expert Comment

by:dotand
ID: 2817385
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
 
LVL 3

Expert Comment

by:darinw
ID: 2822126
Sasha_Mapa repost your comment as an Answer please.

darinw
Customer Service
0
 
LVL 3

Expert Comment

by:darinw
ID: 2822136
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
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 150 total points
ID: 2822503
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
 

Author Comment

by:devender
ID: 2828600
i agree with the answer
thank u
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question