home *** CD-ROM | disk | FTP | other *** search
/ Chip Special: HTML & Java / Chip-Special_1997-01_HTML-a-Java.bin / javasdk / sdk-java.exe / SDKJava.cab / Samples / native_raw / java / JMain.java < prev    next >
Encoding:
Java Source  |  1996-10-10  |  2.6 KB  |  117 lines

  1.  
  2. /*
  3.  
  4. Copyright (c) 1996  Microsoft Corporation
  5.  
  6. */
  7.  
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //
  10. //    JMain
  11. //
  12. //    main class; calls out to natively-implemented methods to perform
  13. //    various operations.
  14. //
  15. ////
  16. public class JMain
  17. {
  18.     // VARIABLES -----------------------------------------------------------
  19.  
  20.     public int iVar1 = 5 ;
  21.     private int[] iCVar2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } ;
  22.  
  23.  
  24.     ////////////////////////////////////////////////////////////////////////
  25.     //
  26.     //    JMain()
  27.     //
  28.     //    constructor; loads our dll which implements native methods.
  29.     //
  30.     ////
  31.     JMain()
  32.     {
  33.         System.loadLibrary("natlib") ;
  34.     }
  35.  
  36.     ////////////////////////////////////////////////////////////////////////
  37.     //
  38.     //    GetiCVar2()
  39.     //
  40.     //    returns (private) array of ints iCVar2.
  41.     //
  42.     ////
  43.     public int[] GetiCVar2()
  44.     {
  45.         return iCVar2 ;
  46.     }
  47.  
  48.     ////////////////////////////////////////////////////////////////////////
  49.     //
  50.     //    GetiVar1()
  51.     //
  52.     //    returns value of instance variable iVar1.
  53.     //
  54.     ////
  55.     public int GetiVar1()
  56.     {
  57.         return iVar1 ;
  58.     }
  59.  
  60.     ////////////////////////////////////////////////////////////////////////
  61.     //
  62.     //    GCUsage()
  63.     //
  64.     //    spawn two threads: JThread1 and JThread2.  JThread2 will block
  65.     //    in native code on JThread1, which will pulse an event object
  66.     //    after it sleeps for the given time.
  67.     //
  68.     ////
  69.     public void GCUsage()
  70.     {
  71.         int    hEvent = w32CreateEvent(0) ;
  72.  
  73.         if (hEvent != 0)
  74.         {
  75.             new Thread(new JThread1(1000, hEvent)).start() ;
  76.             new Thread(new JThread2(hEvent)).start() ;
  77.         }
  78.     }
  79.  
  80.     ////////////////////////////////////////////////////////////////////////
  81.     //
  82.     //    natively-implemented methods
  83.     //
  84.     //    see ..\native\natlib.c for specifics of each method.
  85.     //
  86.     ////
  87.     public native int Square(int iVar) ;
  88.     public native void DoubleInstanceVar() ;
  89.     public native void showMemberArray() ;
  90.     public native void Various() ;
  91.     public native int w32CreateEvent(int iInitState) ;
  92.  
  93.     ////////////////////////////////////////////////////////////////////////
  94.     //
  95.     //    main()
  96.     //
  97.     //    entry point;  executes various methods (implemented in java
  98.     //    and natively) for demonstration purposes.
  99.     //
  100.     ////
  101.     public static void main(String argv[])
  102.     {
  103.         // instantiate our class
  104.         JMain jm = new JMain() ;
  105.  
  106.         // and demonstrate use of native methods
  107.         System.out.println("Square(" + jm.iVar1 + ") = " + jm.Square(jm.iVar1)) ;
  108.         System.out.println("iVar1 before = " + jm.iVar1) ;
  109.         System.out.println("calling DoubleInstanceVar()") ;
  110.         jm.DoubleInstanceVar() ;
  111.         System.out.println("iVar1 after = " + jm.iVar1) ;
  112.         jm.showMemberArray() ;
  113.         jm.Various() ;
  114.         jm.GCUsage() ;
  115.     }
  116. }
  117.