2

次のJavaコードは、メインスレッドに沿って2つのスレッドを作成します。作成された2つのスレッドで、I/O機能を実行するC関数が呼び出されます。次に、メインスレッドから別のC関数(againCallReadFile)が呼び出され、firstThreadメソッドが呼び出されます。この以前に呼び出されたメソッドは、おそらくスリープしているか、まだ機能しています。synchronised methodそれが戻った後、または動作が終了した後、これを確実に呼び出すにはどうすればよいですか?

Javaコード:

package Package;


public class Multithreading {

private native void readFile();
private native void writeFile();
private native void againCallReadFile();

public static void main(String args[]) {
    Multithreading.firstThread(); 
    Multithreading.secondThread();
    // the above functions sleep for 15 seconds
    new Multithreading().againCallReadFile(); // WILL GIVE THE FATAL ERROR ! B'COZ THAT METHOD SLEEPS FOR 15 SECONDS AFTER IT IS CALLED.
}

public synchronized static void firstThread() {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            System.out.println("First Thread Started at " + new java.util.Date());
            System.out.println();
            new Multithreading().readFile();
            try {
                Thread.sleep(15 * 1000); // sleep for 15 seconds
            } catch(Exception exc) {
                exc.printStackTrace();
            }
        }
    };
    new Thread(r,"FirstThread").start();
}

public synchronized static void secondThread() {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            System.out.println("Second Thread Started at " + new java.util.Date());
            System.out.println();
            new Multithreading().writeFile();
            try {
                Thread.sleep(15 * 1000); // sleep for 15 seconds
            }catch(Exception exc) {
                exc.printStackTrace();
            }
        }
    };
    new Thread(r,"Second Thread").start();
}

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

Cコード:

#include "stdio.h"
#include "string.h"
#include "Package_Multithreading.h"

/*
 * Class:     Package_Multithreading
 * Method:    readFile
 * Signature: ()V
 */

void Java_Package_Multithreading_readFile
  (JNIEnv *env, jobject obj) { // function that reads a file

 printf("In the C method that reads file!\n");
 FILE *f_open = fopen("c_io/file_r.txt","r");
 fseek(f_open,0,SEEK_END);
 long lSize = ftell(f_open);
 rewind(f_open);
 // allocate memory to contain whole file
 char *buffer = (char*)malloc(sizeof(char)*lSize);
 size_t result = fread(buffer,1,lSize,f_open); // file is now loaded in the memory buffer
 int i = 0;
 for(i=0;i<strlen(buffer);i++) {
    printf("%c",buffer[i]);
 }
 fclose(f_open);
}

/*
 * Class:     Package_Multithreading
 * Method:    writeFile
 * Signature: ()V
 */

void Java_Package_Multithreading_writeFile
  (JNIEnv *env, jobject obj) { // function that writes a file

 printf("In the C method that writes to the file !\n");
 char buffer[] = "This data is a result of JNI !";
 FILE *f_open = fopen("c_io/file_w.txt","wb");
 int x = fwrite(buffer,1,sizeof(buffer),f_open);
 fclose(f_open);
 printf("Data has been written to the file !");
}

void Java_Package_Multithreading_againCallReadFile
  (JNIEnv *env, jobject obj) { // function that calls the already called synchronised method

  // The following snippet calls the function firstThread of java that could be sleeping. It sleeps for 15 seconds !

  jclass cls = (*env)->GetObjectClass(env,obj);
  jmethodID mid = (*env)->GetMethodID(env,cls,"firstThread","()V");
  (*env)->CallVoidMethod(env,obj,mid); // call the synchronized method that sleeps for 5 minutes !
}

Java_Package_Multithreading_againCallReadFileJavaのメソッドが終了するまで内部のコードを待機させるにはどうすればfirstThreadよいですか、または同期関数を呼び出す前に待機する必要があることを知る方法はありますか?

4

1 に答える 1