020-29133788
    资 讯
    您的位置:首页 >> 资 讯 >> 软件应用 >> 编程开发 >> 正文
    整合c++ thread与 java thread

    点击:   发布日期:2013-01-25

    本文来自 www.020fix.com

    利用gnu g++可以呼叫java的物件,function之间更可以互传资料
    但是c++类别不能继更java类别
    /**
    1:http://sourceforge.net/forum/forum.php?forum_id=500176
    下戴MinGW 5.0.0 之后安装,选取安装g++程gcj
    2:http://gnuwin32.sourceforge.net/packages/libiconv.htm
    下戴Binaries和Developer files,之后解压缩放在和mingw同的个目录
    lib和bn的档案都贴过去 ,设路径在dos下打:set path=c:\mingw\bin
    在mingw下新增一个code的资料夹把以下档案下戴放到code资料夹
    ftp://sources.redhat.com/pub/pth ... t/include/pthread.h
    ftp://sources.redhat.com/pub/pth ... /lib/pthreadGC2.dll
    ftp://sources.redhat.com/pub/pth ... lib/libpthreadGC2.a



    1
    compiler c++
    g++ -c source.cpp
    g++ -o source.exe source.o

    2java转exe

    gcj -C source.java //把产生 class档
    gcjh source //产生source.h (interface)
    gcj -c source.java //把java compile 成 obj //(implement)
    gcj --main=source -o source source.o 把 java obj 变成 exe

    3 c++使用java api (使用cni语法)
    g++ -c source.cpp or gcj -c source.cpp
    gcj -o source.exe source.o -lgcj -lstdc++

    4内嵌assembly language
    using
    __asm("your code/n/t")//online one line
    or
    __asm volatile (
    "code" /n/t
    "code" /n/t
    : :  //extend model
    **/

    底下的程式码放在code资料夹

    /** JavaThread.java **/
    public class JavaThread extends Thread {

      public static int end=0;  
      public void run() {
        try {
          for(int i=0;i<100;i++) {
            System.out.println("Java Thread:"+getName()+" run"+i);
            sleep(1000);
          }
          end++;
        }catch(Exception e) {}
      }
      public static int getInt() {
        return end;
      }

    }command:
    gcj -c JavaThread.java -Thread
    gcj -C JavaThread.java -Thread
    gcjh JavaThread

    /** Thread.cpp **/
    #include <gcj/cni.h>
    #include <iostream>
    #include "JavaThread.h"
    #include "pthread.h"
    using namespace std;
    /** 这是注解与程式无关
    目前只有我和另一位朋友在研究汽车电脑,感于人数不够,无法完成更改
    车子内部的程式的伟大理想,在此徵求同伴研究汽车电脑,意者悄稍话联络
    **/
    void* run1(void*) {
      for(int i=0;i<100;i++)
      cout<<"C++ Thread1:run"<<i<<endl;
    }
    void* run2(void*) {
      for(int i=0;i<100;i++)
      cout<<"C++ Thread2:run"<<i<<endl;
    }
    void* run3(void*) {
      for(int i=0;i<100;i++)
      cout<<"C++ Thread3:run"<<i<<endl;
    }

    void* runJava(void*) {

      JvCreateJavaVM(NULL);
      JvAttachCurrentThread(NULL, NULL);
     
      JavaThread* t1=new JavaThread();
      JavaThread* t2=new JavaThread();
      JavaThread* t3=new JavaThread();

      t1->start();
      t2->start();
      t3->start();  

      while(true) {   
        if(JavaThread::getInt()==3) {
          JvDetachCurrentThread();
          delete t1;
          delete t2;
          delete t3;
          break;
        }
      }
      cout<<"Java Threads end"<<endl;
    }  

    int main() {
      pthread_attr_t attr;
      pthread_attr_init(&attr);
      pthread_attr_setschedpolicy(&attr,SCHED_RR);  

      pthread_t t1,t2,t3,t4;
      pthread_create(&t1,&attr,run1,NULL);
      pthread_create(&t2,&attr,run2,NULL);
      pthread_create(&t3,&attr,run3,NULL);
      pthread_create(&t4,&attr,runJava,NULL);
      pthread_join(t1,NULL);
      pthread_join(t2,NULL);
      pthread_join(t3,NULL);
      pthread_join(t4,NULL);
      cout<<"program end "<<endl;
      return 0;
    }
    command:
    g++ -c Thread.cpp
    gcj -o Thread.exe Thread.o JavaThread.o libpthreadGC2.a -lgcj -lstdc++
    Thread