Suspending and resuming JNI thread

Discussion in 'Java' started by ganeshamutha@gmail.com, Nov 30, 2006.

  1. Guest

    Hi all,,


    I am calling a C native methode from Java in new thread... The
    C function calling and doing everthing fine.. since it is a long
    process i want to quit some time before it is going to terminate
    itself..

    So, i created a window with start, pause, resume and stop
    button...

    when i click the start button, the C code executing
    properly... since it is calling public void run(),

    /* start .....
    {
    nativeclass.itisthread.start();

    }

    but when i calling .suspend() , .resume() for the
    thread to make suspend and resume respectively it is not responding...
    why?

    Say examply... the C code..

    for(;;)
    {
    printf("I am in C.....");
    }


    What my question is .. how can i suspend and resume JNI the
    thread ... Please help me to solve this issue..

    Thanks in Advance,
    ganesh.
     
    , Nov 30, 2006
    #1
    1. Advertising

  2. On 30 Nov 2006 01:45:54 -0800, wrote:
    > What my question is .. how can i suspend and resume JNI the thread


    Thread methods suspend(), resume() etc are deprecated:
    http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

    At any rate, your native thread cannot be forced to suspend or exit,
    it needs to *cooperate* with the controlling thread. The easiest way
    is to periodically call a Java method that can wait on a semaphore.

    One way is to create a new instance of the following class, to pass to
    your native method:

    public class Suspender {
    private boolean cancelled = false;
    private boolean suspended = false;

    public synchronized boolean wait_here() {
    try {
    while (suspended) {
    wait();
    }
    }
    catch (InterruptedException e) {
    cancelled = true;
    }

    return cancelled;
    }

    public synchronized void suspend() {
    suspended = true;
    }

    public synchronized void resume() {
    suspended = false;
    notify();
    }

    public synchronized void cancel() {
    cancelled = true;
    resume();
    }
    }

    From the native method, periodically call wait_here(). If the
    controlling thread calls suspend(), the native thread will block in
    the next call to wait_here(). When the controlling thread calls
    resume(), the native thread will continue.

    If the controlling thread calls cancel(), wait_here() will return
    true, signalling the native method to return.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Nov 30, 2006
    #2
    1. Advertising

  3. Guest

    i, Beaton

    Thanku very much for ur reply, even though i not clear .... i am posted
    sample code exactly what i am using...

    /* Mynative Class***/

    class Mynative implements Runnable
    {
    Thread t;
    Mynative()
    { t=new Thread(this); }

    public synchronized void run()
    {
    Cfun();
    }
    static System.loadLibrary("Clib");
    }

    /*MyGui Class***/
    class GUI extends JDialog implements ActioinListener
    {
    Mynative mm=new Mynative();
    /*Button creation and adding*/
    public void actionPerformed(ActionEvent ee)
    {
    if(ee.getSource()==start)
    mm.t.start(); //C execution started fine...
    and printing in infinite loop
    if(ee.getSource()==pause)
    mm.t.suspend(); // no response... still
    printing..
    if(ee.getSource()==resume)
    mm.t.resume(); // no response.. still printing
    if(ee.getSource()==stop)
    mm.t.stop(); // no response.. still
    printing..
    }
    }


    /*Main*/
    public class Threadwindow
    {
    public static void main(String args[]) {
    GUI window = new GUI();

    window.setTitle("Process_Window");
    window.pack();
    window.show();
    }
    }


    /*Cfun()----in C file--*/

    JNIEXPORT void JNICALL Java_Mynative_Cfun (JNIEnv *env1, jobject
    objj)
    {
    for(;;)
    { printf(" C code :..."); }
    }


    Please explain me .. how i have to change the code inorder to make
    working of suspend, resume and stop with ur code...

    Thanking u,
    Ganesh

    Gordon Beaton wrote:
    > On 30 Nov 2006 01:45:54 -0800, wrote:
    > > What my question is .. how can i suspend and resume JNI the thread

    >
    > Thread methods suspend(), resume() etc are deprecated:
    > http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html
    >
    > At any rate, your native thread cannot be forced to suspend or exit,
    > it needs to *cooperate* with the controlling thread. The easiest way
    > is to periodically call a Java method that can wait on a semaphore.
    >
    > One way is to create a new instance of the following class, to pass to
    > your native method:
    >
    > public class Suspender {
    > private boolean cancelled = false;
    > private boolean suspended = false;
    >
    > public synchronized boolean wait_here() {
    > try {
    > while (suspended) {
    > wait();
    > }
    > }
    > catch (InterruptedException e) {
    > cancelled = true;
    > }
    >
    > return cancelled;
    > }
    >
    > public synchronized void suspend() {
    > suspended = true;
    > }
    >
    > public synchronized void resume() {
    > suspended = false;
    > notify();
    > }
    >
    > public synchronized void cancel() {
    > cancelled = true;
    > resume();
    > }
    > }
    >
    > From the native method, periodically call wait_here(). If the
    > controlling thread calls suspend(), the native thread will block in
    > the next call to wait_here(). When the controlling thread calls
    > resume(), the native thread will continue.
    >
    > If the controlling thread calls cancel(), wait_here() will return
    > true, signalling the native method to return.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Nov 30, 2006
    #3
  4. On 30 Nov 2006 05:01:10 -0800, wrote:
    > Please explain me .. how i have to change the code inorder to make
    > working of suspend, resume and stop with ur code...


    Create an instance of the Suspend class I posted earlier. Pass the
    reference as an argument to your C function (you'll need to change the
    native declaration and re-run javah).

    Now change your C function, so that it occasionally invokes
    susp.wait_here():

    JNIEXPORT void JNICALL Java_Mynative_Cfun(JNIEnv *env1, jobject objj, jobject susp)
    {
    jclass cls = (*env)->GetObjectClass(env1, susp);
    jmethodID mid = (*env)->GetMethodID(env1, cls, "wait_here", "()Z");

    while (!(*env)->CallBooleanMethod(env1, susp, mid)) {
    printf("C code...\n");
    }

    printf("Cancelled!\n");
    return;
    }

    From elsewhere, call susp.suspend(), susp.resume(), or susp.cancel()
    to control the native method.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Nov 30, 2006
    #4
  5. Guest

    Thanku for explain.. but still i am not able to control....

    I am calling the Native thread through start button by calling
    mynative.thread.start();
    C code also altered according to ur suggestion..
    i am creating one more instance of Suspend at GUI class.. and
    for wait i am calling the susp.wait() for suspend click... but it is
    not responding... i am requesting u to please look my code of GUI and
    Mynative class in my previous post...

    one more question is...
    here we are keep on checking in while (C code)... if the
    program calling a one more c function(very lengthy)

    ex,..

    JNIEXPORT void JNICALL Java_Mynative_Cfun(JNIEnv *env1, jobject
    objj, jobject susp)
    {
    a();
    }

    a()
    {
    .....
    ....
    }

    then how can i rewrite the C code or java code inorder to suspend
    and resume , stop click work ..

    Thanking u once again,
    Ganesh.

    Gordon Beaton wrote:
    > On 30 Nov 2006 05:01:10 -0800, wrote:
    > > Please explain me .. how i have to change the code inorder to make
    > > working of suspend, resume and stop with ur code...

    >
    > Create an instance of the Suspend class I posted earlier. Pass the
    > reference as an argument to your C function (you'll need to change the
    > native declaration and re-run javah).
    >
    > Now change your C function, so that it occasionally invokes
    > susp.wait_here():
    >
    > JNIEXPORT void JNICALL Java_Mynative_Cfun(JNIEnv *env1, jobject objj, jobject susp)
    > {
    > jclass cls = (*env)->GetObjectClass(env1, susp);
    > jmethodID mid = (*env)->GetMethodID(env1, cls, "wait_here", "()Z");
    >
    > while (!(*env)->CallBooleanMethod(env1, susp, mid)) {
    > printf("C code...\n");
    > }
    >
    > printf("Cancelled!\n");
    > return;
    > }
    >
    > From elsewhere, call susp.suspend(), susp.resume(), or susp.cancel()
    > to control the native method.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Dec 1, 2006
    #5
  6. On 30 Nov 2006 22:11:07 -0800, wrote:
    > Thanku for explain.. but still i am not able to control....

    [...]

    > then how can i rewrite the C code or java code inorder to suspend
    > and resume , stop click work ..


    You need to be clearer and more specific about what isn't working, or
    what you don't understand.

    I don't know how to be any clearer in my explanation. In your C code,
    you must call susp.wait_here() occasionally. If susp.suspend() has
    been called, it cause susp.wait_here() to *block*. When susp.resume()
    is later called, susp.wait_here() will return and the C function will
    continue.

    You don't need to call susp.wait_here() in a loop, but your original
    example showed a loop. You just need to call it occasionally during
    the lengthy operation.

    I have shown you how to do all of these things. Now it's up to you to
    understand how to put the pieces together in your application. Don't
    ask me to write your code for you.

    I can't help you debug the empty code fragments you've shown: "a()",
    but please do not post a lengthy example either. Try to understand the
    short example I posted earlier.

    Here's a suggestion: try to make it work from Java, you don't need
    native code to test this. Change the run() method to something like
    this:

    public synchronized void run() {
    System.out.println("starting...");

    while (!susp.wait_here()) {
    System.out.println("hello, world");

    try {
    Thread.sleep(1000);
    } catch (InterruptedException e) {
    // ignored
    }
    }

    System.out.println("cancelled");
    }


    > i am creating one more instance of Suspend at GUI class.. and


    Make sure you create exactly *one* instance of the Suspend object,
    that is *shared* by both the GUI code and the native function.

    Alternatively (and perhaps it's not a good idea to suggest
    alternatives at this point) copy the four methods from Suspender.java
    into your MyNative class instead. This way you don't need to create
    any extra objects at all. Call mn.suspend(), mn.resume() from the GUI,
    and mn.wait_here() from the C function.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 1, 2006
    #6
  7. Guest

    I am very clear now... i am following ur alternative apporach.. it is
    seems to be very easy....

    but sorry to say this... still i have problem...

    When i click from GUI it is started and C printf working
    fine....... but when i Click the suspend button the control not able to
    coming out from button.. it is look like going into toggle mode.. even
    JDialog's close button also not working....

    What could be the proplem...... the infinite loop / thread
    issue.. / some thing else.. Please how to rectify it....

    Should i call the wait_here() function more then once in 'C' -
    lengthly flow...


    With many thanks,
    Ganesh..


    Gordon Beaton wrote:
    Gordon Beaton wrote:
    > On 30 Nov 2006 22:11:07 -0800, wrote:
    > > Thanku for explain.. but still i am not able to control....

    > [...]
    >
    > > then how can i rewrite the C code or java code inorder to suspend
    > > and resume , stop click work ..

    >
    > You need to be clearer and more specific about what isn't working, or
    > what you don't understand.
    >
    > I don't know how to be any clearer in my explanation. In your C code,
    > you must call susp.wait_here() occasionally. If susp.suspend() has
    > been called, it cause susp.wait_here() to *block*. When susp.resume()
    > is later called, susp.wait_here() will return and the C function will
    > continue.
    >
    > You don't need to call susp.wait_here() in a loop, but your original
    > example showed a loop. You just need to call it occasionally during
    > the lengthy operation.
    >
    > I have shown you how to do all of these things. Now it's up to you to
    > understand how to put the pieces together in your application. Don't
    > ask me to write your code for you.
    >
    > I can't help you debug the empty code fragments you've shown: "a()",
    > but please do not post a lengthy example either. Try to understand the
    > short example I posted earlier.
    >
    > Here's a suggestion: try to make it work from Java, you don't need
    > native code to test this. Change the run() method to something like
    > this:
    >
    > public synchronized void run() {
    > System.out.println("starting...");
    >
    > while (!susp.wait_here()) {
    > System.out.println("hello, world");
    >
    > try {
    > Thread.sleep(1000);
    > } catch (InterruptedException e) {
    > // ignored
    > }
    > }
    >
    > System.out.println("cancelled");
    > }
    >
    >
    > > i am creating one more instance of Suspend at GUI class.. and

    >
    > Make sure you create exactly *one* instance of the Suspend object,
    > that is *shared* by both the GUI code and the native function.
    >
    > Alternatively (and perhaps it's not a good idea to suggest
    > alternatives at this point) copy the four methods from Suspender.java
    > into your MyNative class instead. This way you don't need to create
    > any extra objects at all. Call mn.suspend(), mn.resume() from the GUI,
    > and mn.wait_here() from the C function.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Dec 1, 2006
    #7
  8. On 1 Dec 2006 04:27:59 -0800, wrote:
    > When i click from GUI it is started and C printf working
    > fine....... but when i Click the suspend button the control not able
    > to coming out from button.. it is look like going into toggle
    > mode.. even JDialog's close button also not working....


    Post the code. I can't help you with the GUI but perhaps someone else
    can.


    > Should i call the wait_here() function more then once in 'C' -
    > lengthly flow...


    Call it everywhere you are willing or able to be suspended.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Dec 1, 2006
    #8
  9. Guest

    Thanku so much for suggestions... now i am able to suspend,resume, the
    C-core since i checking periodically in while loop... (ie, we are
    checking the suspend bool)..

    GUI is also very perfect after i removed the synchronized keyword in
    ur suspend call...

    now i have few more questions...

    Please tell me if C - code written with many function , like,

    JNIEXPORT void JNICALL Java_Mynative_Cfun(JNIEnv *env1, jobject
    objj, jobject susp)
    {
    a();
    }

    a()
    {
    .....
    ....
    b();
    }

    b () { ..... ....... c()}

    c()
    {
    // how can i call the "wait_here", here..
    }

    can i able to pass the JNIEnv *env1 pointer to each functions
    argument.. else can i declare JNIEnv as a globel variable and from the
    extern keyword wait_here() can i able call... Please help me solve
    this little conflict... Please tell me if there any other best way to
    access the wait_here() function in c() function..

    b(), c() are modules each written in different .c files those are
    included very perfectly...

    Thanks again,
    Ganesh
    Gordon Beaton wrote:
    > On 1 Dec 2006 04:27:59 -0800, wrote:
    > > When i click from GUI it is started and C printf working
    > > fine....... but when i Click the suspend button the control not able
    > > to coming out from button.. it is look like going into toggle
    > > mode.. even JDialog's close button also not working....

    >
    > Post the code. I can't help you with the GUI but perhaps someone else
    > can.
    >
    >
    > > Should i call the wait_here() function more then once in 'C' -
    > > lengthly flow...

    >
    > Call it everywhere you are willing or able to be suspended.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Dec 2, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeffrey Hiess

    How can I "peek" at the keyboard without suspending?

    Jeffrey Hiess, Nov 26, 2003, in forum: C Programming
    Replies:
    11
    Views:
    716
    Irrwahn Grausewitz
    Dec 2, 2003
  2. yccheok
    Replies:
    1
    Views:
    616
    Piotr Kobzda
    Mar 28, 2008
  3. Replies:
    5
    Views:
    407
    Arne Vajhøj
    Jun 26, 2008
  4. bgabrhelik
    Replies:
    0
    Views:
    825
    bgabrhelik
    Sep 29, 2009
  5. Ethan
    Replies:
    4
    Views:
    521
    Kevin McMurtrie
    Jun 30, 2010
Loading...

Share This Page