Solved

multithreading in java with native methods

Posted on 1997-06-10
4
495 Views
Last Modified: 2013-11-23
I was wondering if anyone has experience using multiple threads in
Java, where one of the threads calls a native method (c routine).
I seem to be having trouble getting the true multiple thread
performance.

As a simple test, I wrote a c program that prints a line to the screen
once per second. A separate thread (within a graphics window) just
prints a counter in the graphic text window. The problem is, when the
c program gets called (through a button push), the counter is
suspended until the c routine returns. If I comment out the c routine,
and replace it with an identical piece of Java code, everything works
fine (i.e. the printing once per second to the screen doesn't affect
the counter in the graphics text window).

Does anyone have a clue what might be happenning here? It seems like
multithreading should be supported with method calls to c.

thanks alot

Todd Sachs

PS - here is the relevant code that gives me the problem. I've commented out the java code that works fine with multithreading, and only left the c method call - the c code
does exactly what the commented out java loop would do.


import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class GUI extends Frame {
  private native int myc();
  Button
    b1 = new Button("Button 1"),
    b2 = new Button("Button 2");
  TextField
     t = new TextField(10);
  public GUI() {
    setLayout(new FlowLayout());
    addWindowListener(new WL());
    add(b1);
    add(b2);
    add(t);
    Runc testit1 = new Runc();
    testit1.start();
    Counter testit2 = new Counter();
    testit2.start();
  }

  class WL extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.out.println("Window Closing");
      System.exit(0);
    }
  }

  class Counter extends Thread {
    int count = 0;
    boolean runFlag2 = false;

    Counter() {
      b2.addActionListener(new B2());
     }  
 
    public class B2 implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Button 2 pressed");
        runFlag2 = !runFlag2;
      }
    }

    public void run() {
      while (true) {
       if (runFlag2) {
        t.setText(Integer.toString(count++));
        }
        try {
         sleep(100);
        } catch (InterruptedException e){}
       
      }
    }
   }

  class Runc extends Thread {
    int count = 0;
    boolean runFlag1 = false;

    Runc() {
      b1.addActionListener(new B1());
     }  
 
    public class B1 implements ActionListener {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Button 1 pressed");
        runFlag1 = !runFlag1;
      }
    }

    public void run() {
      while (true) {
       if (runFlag1) {

        int stopit = myc();
/*        for (int n=0;n<100;n++) {
          System.out.println("line number: " + (n+1));
          try {
            sleep(1000);
          } catch (InterruptedException e){}
         } */

        runFlag1 = !runFlag1;
        }
        try {
         sleep(100);
        } catch (InterruptedException e){}
       
      }
    }
   }


  static {
   System.loadLibrary("MyImpOfMyc");
  }

  public static void main(String args[]) {
    Frame f = new GUI();
    f.setSize(300,200);
    f.setVisible(true);
  }
}


0
Comment
Question by:tssachs
4 Comments
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
Java's thread implementation is not as good as it could be.

A java thread that calls a native method will block untill the native method returns.

You might want to try having the C thread create its ow thread and the return.

Optionaly, you can bypass this problem (and ecounter others :-) ) by using CORBA or RMI using the  loopback address.
0
 
LVL 4

Expert Comment

by:nil_dib
Comment Utility
I think there is a solution for your problem.
Look in the Sun Java tutorial (http://java.sun.com/docs/books/tutorial/native1.1/implementing/index.html).
There is a chapter called "Threads and Native Methods" with describes the implementation of threads within native methods.
0
 

Accepted Solution

by:
turon earned 150 total points
Comment Utility

A good thing to try would be to execute your C program
from the first ever 100% java shell library:

zaBava.shell: http://www.zaBava.com/ftp/zapShell.zip

Unzip the above package into your classpath,                  then run:
       C:/> java zaBava.shell.main

Once inside the zaBava.shell (a pure java app),
spawn your command in a seperate thread by prepending
the "&" command to before its name:

       z![c:/DOSBIN]> & myC.exe

The next release of the zaBava.shell will feature true
multi-threaded output streams and JDK 1.1.2 compatibility.
The current version is JDK 1.0.2 compliant.

Hope the code helps you,
   ---== Martian ==---
           !!!
            |
0
 

Author Comment

by:tssachs
Comment Utility
Turon,
 
  Unfortunately, I am not running Java on a PC, I am limited to
using a SUN Solaris machine because I need to communicate with
a bit3 board that is mounted on the SUN (Java seems to have
some trouble interfering with the bit3 board as well - but that is another story). Do you have any advice, given that I am using a SUN? I have been able to get something working by simply spawning  a c process from a thread, rather than a nitive method, but communication with this process is pretty slow (through stdin and stdout).

Todd

 
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 81
What is JNDI datasource in spring 1 36
bitbucket vs gitbucket 3 28
github account with ecipse 1 15
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 This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

12 Experts available now in Live!

Get 1:1 Help Now