home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / sdkjava.exe / SDKJava.cab / Samples / native_raw / java / JMain.java < prev    next >
Encoding:
Java Source  |  1998-03-05  |  2.7 KB  |  111 lines

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