Solved

multithreading in java with native methods

Posted on 1997-06-10
4
497 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
ID: 1221520
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
ID: 1221521
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
ID: 1221522

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
ID: 1221523
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
eclipse buid path vs tomcat lib path 10 23
Java string replace 11 45
JavaFX TableView not displaying correctly 3 17
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

770 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