Running different set of methods on the same class via threads

Hello all,
I have this issue that I am trying to deal with. It looks pretty simple to me, but maybe just a mental block I guess :)
Now the code below will give you an idea of what I am trying to achieve. I am not sure if its possible or if there is some other better way to do this. Any help will be appreciated.
Thanks
public class TestClass extends Thread{
     public TestClass(){
     // Assume something goes in here.
     public void abc(){
          System.out.println("In abc");
     public void xyz(){
          System.out.println("In xyz");
     public void run(){
     //What do I put here, since my call is specific.
     public static void main(String args[]){
          TestClass t1 = new TestClass();
          t1.start();// I want t1 to call only abc()
          TestClass t2 = new TestClass();
          t2.start();// I want t2 to call only xyz
}Edited by: mikkin on Mar 12, 2009 3:00 PM

kogose wrote:
you can use the Reflection API to make the desired method an instance variable:Whoa, that's a pretty big case of over-engineering (or under-engineering, depending on how you look at it) and abuse of Reflection. The real problem in the original post is that Thread isn't being used properly. You almost never want to subclass Thread, for reasons like these: your runnable target should be separate from the thread. I would organize your code like this:
public class TestClass {
   public TestClass() {
      // Assume something goes in here.
   public void abc() {
      System.out.println("In abc");
   public void xyz() {
      System.out.println("In xyz");
   public static void main(String args[]) {
      final TestClass t1 = new TestClass();
      final TestClass t2 = new TestClass();
      new Thread(new Runnable() {
         public void run() { t1.abc(); }
      }, "t1Thread").start();
      new Thread(new Runnable() {
         public void run() { t2.xyz(); }
      }, "t2Thread").start();                   
   }

Similar Messages

  • Passing values between methods in the same class

    Hi,
    How to pass internal tables or values between methods in the same class.
    How to check if the internal method in another method is initial or not.
    How to see if the method has already been executed.
    Thanks.

    Just declare the internal table as an attribute in the class - that way every method in this class has access to it.
    Since any method has access to all class attributes you can easily check if the internal table is initial or not.
    I am not aware of any standard functionality if a method has already been executed or not, one way would be to declare a class attribute for each method and set it once the method has been executed. That way every method in that class would know which method has already been executed or not.
    Hope that helps,
    Michael

  • Accessing a private variable from a public method of the same class

    can anyone please tell me how to access a private variable, declared in a private method from a public method of the same class?
    here is the code, i'm trying to get the variable int[][][] grids.
    public static int[][] generateS(boolean[][] constraints)
      private static int[][][] sudokuGrids()
        int[][][] grids; // array of arrays!
        grids = new int[][][]
        {

    Are you sure that you want to have everything static here? You're possibly throwing away all the object-oriented goodness that java has to offer.
    Anyway, it seems to me that you can't get to that variable because it is buried within a method -- think scoping rules. I think that if you want to get at that variable your program design may be under the weather and may benefit from a significant refactoring in an OOP-manner.
    If you need more specific help, then ask away, but give us more information please about what you are trying to accomplish here and how you want to do this. Good luck.
    Pete
    Edited by: petes1234 on Nov 16, 2007 7:51 PM

  • How does a method extend another method in the same class ??

    hi everybody, I want to know if it is possible to have such code as:
    void foo(){
    // foo process
    void bar <<extend>> foo{
    //foo process
    //bar process
    thanks for helping me

    Since you want different names for each of your so-called "child" methods, then neither overloading nor overriding in a subclass will work as each relies on the same method names. Perhaps the following will suffice:
    public class MyClass
       //some stuff
       private myMethodThatDoesSomeStuff()
          //do some stuff that all the other methods must also do
       private methodA(int q, byte z)
          myMethodThatDoesSomeStuff();
          // continue with this method.
       private methodL(short v, float g23)
          myMethodThatDoesSomeStuff();
          // continue with this method.
    }If not, you need to do some design work.

  • How to run different Oracle client versions on the same PC

    I have developped an application (.Net 2.0, ODP v. 10). I must run it on PC where Oracle client version 8 is installed. The PC cannot be upgraded because of a client software running under this version.
    I tried to install ODP v. 10 on this PC in different home, but the software using version 8 stops running.
    Can Oracle Instant Client help ? But which version, 11 ? My application is based on ODP 10. Can ODP 10 access database via Instant Client 11 ?
    Thanks for your help
    LK

    The problem on Windows is: The 10g installation changed the default ORACLE_HOME,now it points to 10g and makes the 8.0.5 application invalid,because it runs now in a much higher environment. What I did in a similar situation, I created a batch file, which sets temporarily the old environment (in my case PATH,ORACLE_SID,ORACLE_HOME) and then invokes the application.
    I don't know whether such a solution will work for you (8.0.5 is very old,desupported since a long time), my old version was 8.1.7.4.
    Werner

  • Can you run different processors in G5 at the same time

    Hey, I have a G5 dual processors, one 1.8 top and 2.0 bottom.
    Can I run them together in a G5?
    Both work properly.
    Thanks

    Well, I found out, in one way it works, but ultimately doesn't.
    With a 1.8 in the top and 2.0 in the bottom, the computer boot up to the startup hard drive but the fans amped up, I assume to avoid overheating.

  • Using values of two different applications that are from the same class

    Hi,
    See the following little program
    What I can't figure out is
    i) How to change the values if I add a button
    ii) How to print just app.x1 etc. The way I have it now, it will just print x1, y1 etc, from both app and app1, but I want to be able to distinguish between the two of them so I can use the values.....ie for example add app.x1 and app1.x2 (only using that as an example of what I want to use them for)
    I'd really appreciate if anyone could help me out, it's probably simple, but just cant figure it.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class drawLine extends JFrame{
    int x1, y1, x2, y2;
    JPanel topPanel;
    JButton button;
    public length l, l3;
    public drawLine(){
    super("drawLine");
    public drawLine(length l){
    this.l = l;
    topPanel = new JPanel();
    button = new JButton("Change");
    topPanel.add(button);
    getContentPane().add(topPanel, BorderLayout.NORTH);
    x1 = 100;
    y1 = 100;
    x2 = l.x2;
    y2 = l.y2;
    System.out.println(x1 + " " + y1 + " " + x2 + " " + y2);
    /*button.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    l.length();
    repaint();
    public void hello(){
    System.out.println(x1 + " " + y1 + " " + x2 + " " + y2);
    public void paint(Graphics g){
    draw(g);
    public void draw(Graphics g){
    g.drawLine(x1, y1, x2, y2);
    public static void main(String args[]){
    length l1 = new length();
    length l2 = (length)l1.clone();
    l1.length();
    l2.length();
    drawLine app = new drawLine(l1);
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app.setSize( 300, 300 );
    app.move(0,0) ;
    app.setVisible( true );
    drawLine app1 = new drawLine(l2);
    app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app1.setSize( 300, 300 );
    app1.move(300,0) ;
    app1.setVisible( true );
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class length implements Cloneable{
    String whatlength;
    int llength;
    int x1 = 100;
    int y1 = 100;
    public int x2, y2;
    public void length(){
    whatlength = JOptionPane.showInputDialog("What is the length of the line");
    llength = Integer.parseInt(whatlength);
    x2 = x1 + llength;
    y2 = y1;
    public Object clone() {
    Object object = null;
    try {
    object = super.clone();
    catch (CloneNotSupportedException exception) {
    System.err.println("AbstractSpoon is not Cloneable");
    return object;
    }

    I've tried it like this, ie by adding an int to drawLine and trying to keep an array of lengths, which sortof works, but see the code I have commented out in the button actionListener class. What I thought I'd ba able to do is access the arrays like that.....how come it gives me an exception?
    public class drawLine extends JFrame{
    int x1, y1, x2, y2;
    JPanel topPanel;
    JButton button;
    public length[] linelength = new length[2];
    public drawLine(final length l, final int k){
    super("drawLine");
    this.linelength[k] = l;
    topPanel = new JPanel();
    button = new JButton("Change");
    topPanel.add(button);
    getContentPane().add(topPanel, BorderLayout.NORTH);
    x1 = 100;
    y1 = 100;
    x2 = linelength[k].x2;
    y2 = linelength[k].y2;
    button.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    //Update the length object int's
    linelength[k].getLineLength();
    // Copy these ints back to the drawline object
    x2 = linelength[k].x2;
    y2 = linelength[k].y2;
    System.out.println(k + " " + linelength[k].x1 + " " + linelength[k].y1 + " " + linelength[k].x2 + " " + linelength[k].y2);
    repaint();
    /*********This bit gives me a null pointer exception ********
    linelength[0].x1 = linelength[1].x1;
    linelength[0].x2 = linelength[1].x2;
    linelength[0].y1 = linelength[1].y1;
    linelength[0].y2 = linelength[1].y2;
    public void paint(Graphics g){
    g.clearRect( 0, 0, getWidth(), getHeight() );
    draw(g);
    public void draw(Graphics g){
    g.drawLine(x1, y1, x2, y2);
    public static void main(String args[]){
    length l1 = new length();
    length l2 = (length)l1.clone();
    l1.getLineLength();
    l2.getLineLength();
    drawLine app = new drawLine(l1, 0);
    app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app.setSize( 300, 300 );
    app.move(0,0) ;
    app.setVisible( true );
    drawLine app1 = new drawLine(l2, 1);
    app1.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    app1.setSize( 300, 300 );
    app1.move(300,0) ;
    app1.setVisible( true );
    }

  • Can I import 2 sets of photos with the same file name numbers?

    I've inadvertantly not had my camera set to "continuous" for file names and now have 2 memory cards with 2 different sets of images with the same file names.
    Can I still import both into Lightroom 2 without problems?
    What are the consequences?
    Thanks
    Stuart

    Yes. Of course, two files with the same name cannot be in the same folder. And don't forget to uncheck Don't import suspected duplicates during import.
    Another thing is that's not a good idea for good asset management, because someday you'll make some mistake because of this ambiguity. Good DAM practices suggest renaming your files after or during import, so that each files has a unique name across your entire image archive.

  • I can't send emails from Mail. I have three different accounts set up and have the same problem with all. The mail just sits in my outbox. All worked fine until Yosemite upgrade. Recent update has not fixed problem.

    I can't send emails from Mail. I have three different accounts set up and have the same problem with all. The mail just sits in my outbox. All worked fine until Yosemite upgrade. Recent update has not fixed problem.

    I have been experiencing the same problem. My mail app won't even open on my Late 2011 Macbook Pro. It will show as open, but the app will never actually show up.

  • CAn I run Lion and snow leopard on the same computer with different sign-ins?

    CAn I run Lion and snow leopard on the same computer with different sign-ins?

    Alternatively, partition your internal HD and dual-boot it. Do note that you have to reboot to switch back and forth. You can't do it by logging out and back in.

  • How do I call methods with the same name from different classes

    I have a user class which needs to make calls to methods with the same name but to ojects of a different type.
    My User class will create an object of type MyDAO or of type RemoteDAO.
    The RemoteDAO class is simply a wrapper class around a MyDAO object type to allow a MyDAO object to be accessed remotely. Note the interface MyInterface which MyDAO must implement cannot throw RemoteExceptions.
    Problem is I have ended up with 2 identical User classes which only differ in the type of object they make calls to, method names and functionality are identical.
    Is there any way I can get around this problem?
    Thanks ... J
    My classes are defined as followes
    interface MyInterface{
         //Does not and CANNOT declare to throw any exceptions
        public String sayHello();
    class MyDAO implements MyInterface{
       public String sayHello(){
              return ("Hello from DAO");
    interface RemoteDAO extends java.rmi.Remote{
         public String sayHello() throws java.rmi.RemoteException;
    class RemoteDAOImpl extends UnicastRemoteObject implements RemoteDAO{
         MyDAO dao = new MyDAO();
        public String sayHello() throws java.rmi.RemoteException{
              return dao.sayHello();
    class User{
       //MyDAO dao = new MyDAO();
       //OR
       RemoteDAO dao = new RemoteDAO();
       public void callDAO(){
              try{
              System.out.println( dao.sayHello() );
              catch( Exception e ){
    }

    >
    That's only a good idea if the semantics of sayHello
    as defined in MyInterface suggest that a
    RemoteException could occur. If not, then you're
    designing the interface to suit the way the
    implementing classes will be written, which smells.
    :-)But in practice you can't make a call which can be handled either remotely or locally without, at some point, dealing with the RemoteException.
    Therefore either RemoteException must be part of the interface or (an this is probably more satisfactory) you don't use the remote interface directly, but MyInterface is implemented by a wrapper class which deals with the exception.

  • We just got 2 iPad air today and Set up both with the same Apple ID, now I'm getting messages and FaceTime for my 13 yr old daughter.  Do I have to create a different Apple ID for her?

    We just got 2 iPad air today and Set up both with the same Apple ID, now I'm getting FaceTime and messages for my 12 yr. old.  Do I have to set up a separate Apple ID  for her to avoid this?

    It's a trust issue. You can set them up so you don't receive each other's messages as long as you each have your own email address.
    The trust issue is the fact that either of you can change the separation at any time via Settings by checking the appropriate (inappropriate?) email address.
    To be absolutely sure you can never receive each other's messages will require two Apple IDs.

  • I have a production mobile Flex app that uses RemoteObject calls for all data access, and it's working well, except for a new remote call I just added that only fails when running with a release build.  The same call works fine when running on the device

    I have a production mobile Flex app that uses RemoteObject calls for all data access, and it's working well, except for a new remote call I just added that only fails when running with a release build. The same call works fine when running on the device (iPhone) using debug build. When running with a release build, the result handler is never called (nor is the fault handler called). Viewing the BlazeDS logs in debug mode, the call is received and send back with data. I've narrowed it down to what seems to be a data size issue.
    I have targeted one specific data call that returns in the String value a string length of 44kb, which fails in the release build (result or fault handler never called), but the result handler is called as expected in debug build. When I do not populate the String value (in server side Java code) on the object (just set it empty string), the result handler is then called, and the object is returned (release build).
    The custom object being returned in the call is a very a simple object, with getters/setters for simple types boolean, int, String, and one org.23c.dom.Document type. This same object type is used on other other RemoteObject calls (different data) and works fine (release and debug builds). I originally was returning as a Document, but, just to make sure this wasn't the problem, changed the value to be returned to a String, just to rule out XML/Dom issues in serialization.
    I don't understand 1) why the release build vs. debug build behavior is different for a RemoteObject call, 2) why the calls work in debug build when sending over a somewhat large (but, not unreasonable) amount of data in a String object, but not in release build.
    I have't tried to find out exactly where the failure point in size is, but, not sure that's even relevant, since 44kb isn't an unreasonable size to expect.
    By turning on the Debug mode in BlazeDS, I can see the object and it's attributes being serialized and everything looks good there. The calls are received and processed appropriately in BlazeDS for both debug and release build testing.
    Anyone have an idea on other things to try to debug/resolve this?
    Platform testing is BlazeDS 4, Flashbuilder 4.7, Websphere 8 server, iPhone (iOS 7.1.2). Tried using multiple Flex SDK's 4.12 to the latest 4.13, with no change in behavior.
    Thanks!

    After a week's worth of debugging, I found the issue.
    The Java type returned from the call was defined as ArrayList.  Changing it to List resolved the problem.
    I'm not sure why ArrayList isn't a valid return type, I've been looking at the Adobe docs, and still can't see why this isn't valid.  And, why it works in Debug mode and not in Release build is even stranger.  Maybe someone can shed some light on the logic here to me.

  • We have 2 ipods & would like to sync different songs to each using the same Itunes. How do we accomplish this?

    We have 2  ipods & would like to sync different songs to each using the same itunes library. How do we accomplish this?

    Use one of the several outlined methods in this Apple support document. I personally like the individual playlist set up where you add everything you want to a specific playlist and then set up each iPod to sync that one playlist that contains all the content you would like to be synced to it.
    How to use multiple iPods, iPads, or iPhones with one computer
    B-rock

  • Why can't two different users use Itunes on the same computer with different log ins

    Why can't two different users use Itunes on the same computer with different log ins

    Pmorgan5672 wrote:
    Why can't two different users use Itunes on the same computer with different log ins
    They can, but not at the same time.
    If one user left iTunes running, and another user tries to use iTunes from a different Windows user ID, that second user will get an error message.  If they really want to use iTunes, they either have to beg the first user to log in and close iTunes, or else they have to restart the computer.
    If you are asking the technical reason why, it is something about context switching.

Maybe you are looking for

  • Third party free sample sale process

    Dear All,              We are sending free sample materials for testing to a customer, Material will be dispatched through Third party Vendor. How to do MM & SD Process from excise point of view. Note: Material directly will get dispatch from vendor

  • Sequence number for group of values in column

    I need to create a sequnece number in a column starting from 5 and incremented by 5, based on values presenet in first column. If the value in first column changes then the sequence should start again with 5 and increment by 5. for example: I want to

  • How can I upgrade from tiger 10.4.11 to Lion?

    i have a Macbook and its running tiger 10.4.11, how can i upgrade it to Mac os x lion ?

  • How do I turn a Smart Shape into a Button?

    Using Version 7, on a Content Master Slide how do I turn a Smart Shape into a Button?

  • Illustrator CS6 Crashes on Mac

    Greetings, dear Adobe support crew! Illustrator crashes I have two copies of Adobe Design Standard Ukrainian version. I've installed them on two almost similar Macs. And everything is fine, except the copy of Illustrator on one of the computers. The