Setjmp/longjmp from threads

Can I use setjmp() and longjmp() calls in a multithreaded application?
The man pages say these are not thread safe.
Thanks,
Bhaskar

Everything in java is running in a thread. Therefore the answer to your question is YES.
matfud

Similar Messages

  • Setjmp/longjmp, threads and bdb

    We are developing a multithreaded transactional server in C that
    uses bdb. We are considering an exception handling mechanism based
    on setjmp/longjmp. The intention is to centralize error handling,
    including database-related errors. We were wondering if other bdb users
    have had experience with bdb/multi-threading/setjmp,longjmp?
    Are there any interactions we should be weary of?
    Thanks,
    Guy

    geert3 wrote:
    I was facing the same problem: JNI function calls longjmp, then VM aborts when exiting from JNI function back into Java.
    Turns out to be a problem in Java, see http://bugs.sun.com/bugdatabase/view_bug.do%3Bjsessionid=43ac0833a910963aef55e681c2cd?bug_id=6649401
    I give up. How does that bug or the one that was actually fixed (since that was a duplicate) fix anything at all associated with longjmp?
    Not to mention of course that using setjmp/longjmp in C correctly is something that requires very, very careful coding. I certainly wouldn't consider it likely nor even reasonable for it to work when crossing boundaries with even relatively simple third party invocations and certainly not something like java.

  • Throw exceptions from threads?

    Is it possible to throw exceptions from threads since I can't add the throws statement to the run method I don't know how I could do it...
    Any help greatly appriciated!
    Cya

    Is it possible to throw exceptions from threads since
    I can't add the throws statement to the run method I
    don't know how I could do it...
    Any help greatly appriciated!
    CyaYou can't throw checked exceptions, because obviously the code responsible for invoking the run() method wouldn't know what to do with it, and you wouldn't be the one to catch them. You can however throw unchecked exceptions (such as those based on RuntimeException).

  • Problem with Serialization from Threads

    I'm quite new to java threads, and i'm doing a simple server.
    It opens a thread for every socket connection that is opened, so the Socket is extern to the Thread.
    When I try to serialize on the socket a Vector (that contains Vectors, etc..) that is extern to the Thread, the first time everything goes ok, but when the thread modifies some variables in some objects contained in a vector(that, i repeat is extern to the thread), the serialized object doesn't seem to change, and the client continues receiving the same variables, without change.
    In a first time i thought that maybe creating a thread, the second one took a copy of the space of addresses of the main one(and so when i modify a variable from a thread the variable in the main thread didn't change), but i read that java uses the same space of addresses for every thread, so i don't really know what it could be.
    Maybe i've not been clear so i make an example:
    MyType is a class containing some fields, let's put x and y
    In the class MainClass i have an istance of this class, and also a socket and from here i open a thread of class MyThread
    MyThread modifies x and y in a object of MyType contained in the "3D array" that is in MainClass
    Then from that thread i serialized on the socket (which was been passed by MainClass).
    Well, doing this way the vector that the client receives has always the x and y fields unchanged.
    Please help me, cause i've been thinking all day of what it could be and found nothing on the web.
    Also sorry for my poor english.

    I thought that could be the problem, but it wasn't.
    The client still receives the vector with objects
    unchanged.
    I think the problem is that the thread can only access
    to a copy of the class that contains the vector, and
    so when i send the real one, it isn't changed.not likely. one of the things about threads is that they share everything but local (read: non-Object) data. All objects are accessible to any thread of your application, as long as that thread has a reference to it.

  • How to get value from Thread Run Method

    I want to access a variable from the run method of a Thread externally in a class or in a method. Even though I make the variable as public /public static, I could get the value till the end of the run method only. After that it seems that scope of the variable gets lost resulting to null value in the called method/class..
    How can I get the variable with the value?
    This is sample code:
    public class SampleSynchronisation
    public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run method "+sathr.x);
    /* I should get Inside the run method::: But I get only Inside */
    class sampleThread extends Thread
         public String x="Inside";
         public void run()
              x+="the run method";

    I think this is what you're looking for. I hold up main(), waiting for the results to be concatenated to the String.
    public class sampsynch
        class SampleThread extends Thread
         String x = "Inside";
         public void run() {
             x+="the run method";
             synchronized(this) {
              notify();
        public static void main(String[] args) throws InterruptedException {
         SampleThread t = new sampsynch().new SampleThread();
         t.start();
         synchronized(t) {
             t.wait();
         System.out.println(t.x);
    }

  • How can I get the variable with the value from Thread Run method?

    We want to access a variable from the run method of a Thread externally in a class or in a method. Even though I make the variable as public /public static, I could get the value till the end of the run method only. After that scope of the variable gets lost resulting to null value in the called method/class..
    How can I get the variable with the value?
    This is sample code:
    public class SampleSynchronisation
         public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run method "+sathr.x);
    // I should get Inside the run method::: But I get only Inside
    class sampleThread extends Thread
         public String x="Inside";
         public void run()
              x+="the run method";
    NB: if i write the variable in to a file I am able to read it from external method. This I dont want to do

    We want to access a variable from the run method of a
    Thread externally in a class or in a method. I presume you mean a member variable of the thread class and not a local variable inside the run() method.
    Even
    though I make the variable as public /public static, I
    could get the value till the end of the run method
    only. After that scope of the variable gets lost
    resulting to null value in the called method/class..
    I find it easier to implement the Runnable interface rather than extending a thread. This allows your class to extend another class (ie if you extend thread you can't extend something else, but if you implement Runnable you have the ability to inherit from something). Here's how I would write it:
    public class SampleSynchronisation
      public static void main(String[] args)
        SampleSynchronisation app = new SampleSynchronisation();
      public SampleSynchronisation()
        MyRunnable runner = new MyRunnable();
        new Thread(runner).start();
        // yield this thread so other thread gets a chance to start
        Thread.yield();
        System.out.println("runner's X = " + runner.getX());
      class MyRunnable implements Runnable
        String X = null;
        // this method called from the controlling thread
        public synchronized String getX()
          return X;
        public void run()
          System.out.println("Inside MyRunnable");
          X = "MyRunnable's data";
      } // end class MyRunnable
    } // end class SampleSynchronisation>
    public class SampleSynchronisation
    public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run
    method "+sathr.x);
    // I should get Inside the run method::: But I get
    only Inside
    class sampleThread extends Thread
    public String x="Inside";
    public void run()
    x+="the run method";
    NB: if i write the variable in to a file I am able to
    read it from external method. This I dont want to do

  • How can I get the variable with the value from Thread's run method

    We want to access a variable from the run method of a Thread externally in a class or in a method. Even though I make the variable as public /public static, I could get the value till the end of the run method only. After that scope of the variable gets lost resulting to null value in the called method/class..
    How can I get the variable with the value?
    This is sample code:
    public class SampleSynchronisation
         public static void main(String df[])
    sampleThread sathr= new sampleThread();
    sathr.start();
    System.out.println("This is the value from the run method "+sathr.x);
    /* I should get:
    Inside the run method
    But I get only:
    Inside*/
    class sampleThread extends Thread
         public String x="Inside";
         public void run()
              x+="the run method";
    NB: if i write the variable in to a file I am able to read it from external method. This I dont want to do

    Your main thread continues to run after the sathr thread is completed, consequently the output is done before the sathr thread has modified the string. You need to make the main thread pause, this will allow sathr time to run to the point where it will modify the string and then you can print it out. Another way would be to lock the object using a synchronized block to stop the main thread accessing the string until the sathr has finished with it.

  • Divide file contents and read from threads

    I am reading a file which contains doubles... I can do this with 1 thread.
    Now I want to use 2 threads and make each thread read half of that file.. How do I make file to half .. If I take the length and divide by two wont it destort the doubles?
    public class SingleThread extends Thread
         /*** Constructor ***/
         public SingleThread(){}
         /*** Run the thread ***/
         public void run()
              try{
              FileInputStream fis = new FileInputStream("File1.bin");
              DataInputStream dis = new DataInputStream(fis);
              int size = fis.available();
              System.out.println("Size of File1 "+size);
              /*Create an array that can store 100,000 numbers*/
              double num1[] = new double[100001];
              /*Get random number*/
              for (int count = 0; count <= 100000; count ++)
                   num1[count] = dis.readDouble();
                   System.out.println(count); //Count
                   System.out.println(num1[count]); //Prints doubles that are read from file.
         }//try
              catch(Exception e){System.out.println("Exception: "+e.getMessage());}
         /******************** Main Program ********************************************************/
         public static void main(String[] args)
              new SingleThread().start();
    }

    I dont know....my professor wants us to test multithreading response times.. :(

  • Problem update JTextArea with message from thread

    I have a JTextArea to present the actually status of an running thread. The thread sends strings like: System.out.println("Reading in new message...");to the standardoutput.
    The problem is, that my application freezes when it should update the JTextArea. Any idea and what type of actionListener is recommend to use for the JTextArea?
    try
       BufferedReader breader = new BufferedReader(new InputStreamReader(System.in));
       statusmsg = breader.readLine();
       statusTextArea.append(statusmsg);
    catch (Exception e){};Cheers

    In general, you can't update the Swing stuff from "side" threads. Side threads talk with the Swing event thread by creating Runnable objects which contains the necessary data (in your case, for instance, the JTextArea or its parent panel/window, and the text to append), and then invoking SwingUtilities.invokeLater() to have this object run() method called by the Sing event thread. See:
    http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html

  • Is it possible to invoke JEB method from thread?

    Hello everybody!
    I have problem creating EJB from the thread. Here is the part of code below:
    public void email() throws Exception {
    class MailThread implements Runnable {
    public MailThread() {
    public void run() {
    try {
    String jndiName "java:comp/env/ejb/PROG/Mail";
    IMailHome mailHome;
    IMail mail;
    InitialContext context = new InitialContext();
    Object objref = context.lookup(jndiName);
    mailHome = (IMailHome) PortableRemoteObject.narrow(objref, IMailHome.class);
    // Exception here:
    // java.rmi.RemoteException: Exception Substitute; nested exception is:
    // java.lang.NullPointerException mail = mailHome.create();
    // business metods
    // mail.method1();
    } catch (Exception e) {
    System.out.println("Exception:" + e);
    MailThread mt = new MailThread();
    Thread thread = new Thread(mt);
    thread.start();
    Exception:
    java.rmi.RemoteException: Exception Substitute; nested exception is:
    java.lang.NullPointerException
    I am not able to create EJB instance from the thread. The same code works well in servlet. Am I doing something wrong?
    May be there are some different approaches to implement some operations asynchronously at EJB level do not using JMS and MDB? J2EE specification prohibits to use threads inside EJB so the only way I see is to create different thread on servlet side and invoke EJB methods from it. But it seems that this doesn't work too. Could anyone help me?
    Thanks in advance,
    Vadim Lotarev

    If the passcode will not work, the only alternative is to restore the phone as new. You cannot change the passcode from the lock screen.

  • Data transfer from thread to thread?

    Hi everyone,
    I have the following problem: I have a program that has a thread that is receiving and sending data using NIO. When data arrives, the NIO thead passes the data to my main thread, who's doing a lot with it. The problem is, that the NIO thead has to wait till the data is processed (the method returns) in the main thread. How can I get arround this? I want my NIO thread to not wait on anything just pass data to somewhere and return immediately.
    I was thinking of a kind of cubby hole class where the nio thread could pass the data and then notify the main thread that data is available. The only problem with this is, that I can't send my main thead to sleep with wait() since it's also responsible for the interface! Do I really need another thread, a helper thread, that waits for the data and passes it to my main thread or am I thinking totally wrong here?
    Thanks for any help!
    best regards,
    Chris

    Thanks for your answers, I'm trying to clarify a few things:
    My programm is a dual server. It has to talk to one client on one side and to multiple clients on the other (2 different ports ;). My thread architecture is as follows: In my main class (with the main method) I'm building the guy and starting the 2 server threads. One is using normal blocking IO and is responsible for the single client (which is a billing server) and the other thread is using NIO to handle the other clients. For each client there's also a client thread which handles some external scripts which display some information remotely to the clients (the clients are just some old 486 which are only used as displays). The client threads are handled by an object in the main object.
    Right now whenever I receive something from a client, I'm passing it to my main object. The main object displays the information using tables (I have my own tablemodel classes too) and then, depending on what data I got, it has to start some actions on the client threads. So the data arrives, goes to the main threads, is displayed and goes to the client thread it belongs too. And while this happens, the nio thread has to wait.
    The problem is that I think that sometimes it takes to long for the whole data processing and my clients time out (10 secs). I have a timer running on the NIO thread which checks for a ping every 10 seconds and sends out a new one if the old one came back. Otherwhise the connection is closed.
    Thanks for reading :)
    Chris

  • Results returned directly from Thread Pool?

    Hi there. I wonder is there any example showing an Applicating which use thread pool to process multiple requests and return the results directly back to the client.
    Eg:
    When calling int doProcess(Runnable process), how can I get some value return from that method?
    Most example that I've seen were just showing a void run() method with some print statements.

    you'll need to use synchronized threads in your run method to ensure that the proper value is returned. look for synchronized thread examples. most examples using one or two threads run in a run() method are gibberish and have no thought put into them.

  • Accesing mysql database from threads

    Hi
    dont know if this is the right place for this
    I'm building a server-client application, on sever's side I create a thread for every client who can request several operations over a mysql database (insert, delete, update, etc), at some point 2 clients can request the same operation over the same registry (2 clients trying to delete the same registry, for example), I try to avoid that to happend, so before doing any operation over the database I check the correct status (a registry must exist to delete, or not exist to create, for example), this is the code that makes that (for inserting a registry):
    //first I make sure the registry does not exist
    sql = "SELECT id_grupo FROM adm_grupo WHERE nombre_grupo = '" + nomGrupo + "'";
    res = bd.ConsultarSQL(sql);
    //If already exist, the operation is rejected
    if (res.next()) {
        EnviarError("Error al Crear el Grupo: Ya existe ese nombre");
         return -1;               
    //Check point ends
    //If not, the insert statement is made
    sql = "INSERT INTO adm_grupo VALUES (NULL,'" + nomGrupo + "','N')";however, since it could be 2 threads (every thread is an object) doing that, the first one can pass the check point and stop because its processor time is over, so the second can start its insert process and pass the check point too (because the first one didn't have enough time to insert the registry), then both registries are inserted causing a logical error, I thought in fixing that problem using the synchronized keyword for the functions, but I read that keyword blocks 1 object that can be accesed for several threads, but in my case I have several threads, everyone accesing its own object, so I'm not sure if the synchronized will work, I dont know any other solution to this,
    thnx in advance for any help

    Sure.  But test it.  You'll need to test it regardless, just to make sure it all works.
    In general, I usually prefer to have the backup tool create the initial SQL database archive on the primary server, zip it, and to then have the backup server in a trusted network location pull the database archive over to the backup server.  (It can then unzip it or process it as needed.)  With this, the primary server doesn't have access to the backups, nor to the backup server. This intended to isolate the access that a breach of the primary server might permit.

  • Ejb lookups fail from threads spawned from servlet

    Hello,
    We have a servlet from which we are spawning a thread. In the thread we are
    trying to do a JNDI lookup for the database resource / EJBs. It does not
    work as if it is not able to find the context t do the lookup.
    I thought that the threads are spawned in the same JVM and context, so why
    does the lookup not work.
    Please need to figure out a solution to this problem, we need to call some
    business logic in the EJBS from the thread and we do not want to do RMI-IIOP
    lookups becuase they could be slower.
    Appreciate your help in advance.
    Regards,
    Sakib

    Custom threading is not supported. I think a workaround was posted on
    this alias (search for custom threading), where you can call the
    proprietary API's of the container to recreate the context, but I'm
    pretty sure that it's not a supported or recommended solution.
    The better (and more scalable) solution is to avoid spawning your own
    threads.
    David
    Sakib Mehasanewala wrote:
    Hello,
    We have a servlet from which we are spawning a thread. In the thread we are
    trying to do a JNDI lookup for the database resource / EJBs. It does not
    work as if it is not able to find the context t do the lookup.
    I thought that the threads are spawned in the same JVM and context, so why
    does the lookup not work.
    Please need to figure out a solution to this problem, we need to call some
    business logic in the EJBS from the thread and we do not want to do RMI-IIOP
    lookups becuase they could be slower.
    Appreciate your help in advance.
    Regards,
    Sakib

  • Urgent - calling EJB methods from thread

    Dear all
    I am aware of EJB spec that does not permit developer managed threads within EJB components. However, I need to call EJB methods from my own thread. Hence, it there any way to to call EJB methods from developer managed thread ?? If yes, please advice me, thanks in advance and your help will be appriciated.
    Hava a nice day

    You can call EJB's from your threads. Your thread will act as the client and the call will to EJB will be same as any other client invoking EJB's. Hope I understand your question !.

Maybe you are looking for