Solved

java multithreading

Posted on 2000-05-07
20
403 Views
Last Modified: 2013-11-23
is multithreading performed automatically or programmer has to implement
0
Comment
Question by:devender
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now