Download Shareware and Freeware Software for Windows, Linux, Macintosh, PDA

line Home  |  About Us  |  Link To Us  |  FAQ  |  Contact

Serving Software Downloads in 976 Categories, Downloaded 31.015.061 Times

GCJ::Cni::Examples 0.03

  Date Added: March 13, 2010  |  Visits: 1.092


Report Broken Link
Printer Friendly Version

Product Homepage
Download (83 downloads)

GCJ::Cni::Examples is a Perl module with examples of how to use GCJs CNI interface to write Perl Modules in Java. EXAMPLES Writing Treaded modules in Java One benefit of using GCJ is that it takes advantage of POSIX threading. This is nice since Perls threading model is, shall we say, less than ideal. Now, you could theoretically write a module in C or C++ and and use the standard POSIX library to do your threaded work for you, however, why not take advantage of the nice Threading interface that Java provides to you by default. This may also come in handy if you have a massively threaded Java library that youd like to call from Perl. For this example assume that I want to do matrix multiplication, albeit in a very crazy manner. Therefore, I will write a Java class, Matrix, and a method, multiply, which will take another matrix to perform the operation between. Heres the crazy part, for each cell in the resulting matrix I will spawn a new thread, as an internal class, and give it a row and a column from which they will derive the result. I know this is a little wierd but the point is that Ill be spawning a lot of new threads. To multiply two 10x10 matrices we will have to spawn 100 threads (one thread per cell in the resulting 10x10 matrix). Imagine doing this in Perl, then imagine doing it in Java; natively compiled Java. Much faster and a lot less headaches. The Java Interface/pseudocode is as follows: public class Matrix { public Matrix ( int numRows, int numCols ) { ...Constructor stuff... } public void set ( int row, int col, int val ) { ...set an element... } public int get ( int row, int col ) { ...get an element... } public Matrix multiply ( Matrix times ) { ...set it up... for ( int i = 0; i < rows; i++ ) { for ( int j = 0; j < times.getCols(); j++ ) { //GO NUTS!!!! ArrayMultiplier multiplier = new ArrayMultiplier(this, times, result, i, j); multiplier.start(); ...etc... } } ...wait for the threads to exit and return the new matrix... } public int getRows ( ) { ...get number of rows... } public int getCols ( ) { ...get number of columns... } public void print ( ) { ...print the matrix to stdout... } private class ArrayMultiplier extends Thread { public ArrayMultiplier ( Matrix a, Matrix b, Matrix result, int row, int col ) { ...Construct this bad boy... } public void run ( ) { int sum = 0; for ( int i = 0; i < a.getCols(); i++ ) { sum += (a.get(row, i) * b.get(i, col)); } result.set(row, col, sum); } } } Now, we want to call this class from Perl. My perferred manner is through SWIG so thats what were going to use. However, you do not need to use SWIG, you can use whatever method you prefer when wrapping C++ classes. We begin by creating a C++ header file from our above class. This is done by using GCJs gcjh utility. First we need to class compile gcj -C Then we go ahead and create the header file: gcjh Matrix Easy enough. We can then extract the interface we want to have available in Perl from the generated header file and from it create a i file to be used as input to SWIG. I usually start by copying my header file to the same named file but with an i extension instead. I then remove all of the grimy C++ gruff, private methods and variables, slap a module directive on it and call it done. It wont always be this easy though. When youre done an interface file for the above class should look something like this: %module Matrix; typedef int jint; class Matrix { public: Matrix (jint, jint); virtual void set (jint, jint, jint); virtual jint get (jint, jint); virtual ::Matrix *multiply (::Matrix *); virtual jint getRows (); virtual jint getCols (); virtual void print (); }; We then put SWIG to work and generate our C++ wrapper for Perl: swig -perl -c++ Matrix.i After the above command we now will see two new files in our current directory, and Matrix_wrap.cxx; these correspond the module directive we gave in Matrix.i. At this point all we have left to do is compile, and use. gcj -c gcc -c -I -include Matrix.h Matrix_wrap.cxx gcc -shared -lgcj -lstdc++ Matrix.o Matrix_wrap.o A simple Perl file using this module might look something like: sub populate_matrix { my $matrix = shift; for ( my $i = 0; $i < $matrix->getRows(); $i++ ) { for ( my $j = 0; $j < $matrix->getCols(); $j++ ) { $matrix->set($i, $j, $i * $j); } } } use GCJ::Cni; use Matrix; GCJ::Cni::JvCreateJavaVM(undef); GCJ::Cni::JvAttachCurrentThread(undef, undef); my $matrix = new Matrix::Matrix(10, 10); populate_matrix($matrix); my $matrix2 = new Matrix::Matrix(10, 10); populate_matrix($matrix2); $matrix3 = $matrix->multiply($matrix2); $matrix3->print(); GCJ::Cni::JvDetachCurrentThread(); Ill leave it up to the reader to write a Perl module that does the same thing, that is, spawns 100 threads. In my own personal fiddling I found that just spawning that many threads (never mind doing any kind of work) was over 3 time slower in Perl. In this situation we at least get to use our favorite language, Perl, and offload some of the heavy hitting to a language more suited for it. Where this really comes in handy is when you have an existing multi-threaded Java library that you would like to expose to Perl. You could theoretically natively compile various components of your library and then generate Perl bindings to access it. In this way, you get a little extra speed and efficiency as well as code reuse and binding..

Requirements: No special requirements
Platforms: Linux
Keyword: Arraymultiplier Gcj Gcjcniexamples Gcjs Gcjs Cni How To Int Java Matrix Module Perl Perl Module Perl Modules Public Swig
Users rating: 0/10

License: Freeware Size: 20.48 KB
Libraries  -  Java::Import::Examples 0.03
Java::Import::Examples is an example of how to use Java::Import to call into Java classes. Making RMI calls from Perl One nice thing about Java is the extremely straight forward manner in which it allows you to make calls to remote objects...
28.67 KB  
Network Management Tools  -  How To Make A Resume 1.0
A quick and easy guide on how to make a resume. Includes tips and tricks on cover letters and job interviews too. Learn how to write a resume that will get noticed for all the right reasons. Whether you are thinking of changing jobs or have just...
1.58 MB  
Home & Leisure  -  How To Make A Candy Bouquet 1.0
eBook showing how to make a candy bouquet, with pictures and detailed instructions.
768 KB  
Teaching Tools  -  How to draw a horse 5
How to draw a horse drawing game. A drawing activity for kids centered around their horse. In this section you will be learning how to draw a horse. You will draw the basic shapes to help you draw the horse. how to draw a face drawing game. free /...
10.24 KB  
Networking Tools  -  How To SEO Your Website 1.0
How To SEO Your Website in the Search Engine. Steps you can take to get Ranked. The best way to set up a website to get good internet search engine placement. Get more website traffic by making your site more attractive to Google and other search...
37 KB  
E-Mail Tools  -  How to Open OST File Free 1.0
Do not worry if you are looking for a reliable solution about how to open ost file, you have already found the response. The service of Microsoft Outlook recovery, provided by How to Open OST File Free, represents the fastest way out if you...
1.86 MB  
Information Lookup  -  How To Build Muscle 1.0
How to build muscle fast and avoid skinny guy mistakes. Free bodybuilding ebook that reveals the top 20 ways to screw up in the gym. Avoid these mistakes that almost every skinny guy makes.
2.49 MB  
Screen Savers  -  Stock Market How To 1.0
Stock Market How To - Learn more about the stock market and how to make money trading the market at homepage
5 MB  
Utilities  -  How to change folder icon 1.4
How to change folder icon? Use this handy shell extension that enables you to quickly change folder icons to indicate a priority level or project status, or you can use it to mark a folder with a special icon or color of your choice. Right-click...
985 KB  
Development Editors  -  Screws 1.3 Build 9395
Screws is a teaching example for how to use Java Canvas, fillRect, fillOval, fillPoly and AffineTransform to draw. Java source is included for you to cannibalise. It draws 8 types of screw heads. Give Screws a try to see what it's really capable...
Programming  -  FLEX-db Digital Asset Manager 3.0.9
FLEX-db - an enterprise Digital Asset Manager (DAM). It ingests and links metadata with files, creates thumbnails, and processes files using business rules. FLEX-db has a JSP client, Java app server for file input and output and an EJB metadata...
21.57 MB  
Programming  -  Libicom 0.9.0
The libicom library is a character based dynamicly linked library for Linux. It is used to remotely control the Icom IC-R8500 wide band receiver via an RS232 link. All call and return parameters to the control functions are character string based....
20.48 KB  
Programming  -  dotdesktop 0.3
Dotdesktop library provides ability to parse desktop entry file and access the information in a convenient way. Desktop entry file format is defined by, it is used to describe information about an application such as the name and...
327.68 KB  
Programming  -  Cedalion for Linux 0.2.6
Cedalion is a programming language that allows its users to add new abstractions and define (and use) internal DSLs. Its innovation is in the fact that it uses projectional editing to allow the new abstractions to have no syntactic limitations.
471.04 KB  
Programming  -  libyasl 0.2
Libyasl is a C++ class library to easily realize TCP/UDP/Multicast clientsand servers in IPv4 and IPv6 environments under GNU/Linux systems.
143.36 KB  
Libraries  -  wolfSSL 3.13.0
The wolfSSL embedded SSL/TLS library is a lightweight SSL library written in ANSI standard C and targeted for embedded and RTOS environments - primarily because of its small size, speed, and feature set. It is commonly used in standard operating...
3.88 MB  
Libraries  -  EuGTK 4.8.9
Makes it easy to develop good- looking, fast, cross-platform programs that run on Linux, OS X, and Windows. Euphoria is a very fast interpreted/compiled language with straight-forward syntax. EuGTK allows programming in a clean, object-oriented...
10.68 MB  
Libraries  -  Linux User Group Library Manager 1.0
The LUG Library Manager is a project to help Linux User Groups start their own library. A LUG library is helpful to the community at large because it increases access to information, and gives everyone the opportunity to become more knowledgeable.
5.35 KB  
Libraries  -  Module::MakefilePL::Parse 0.12
Module::MakefilePL::Parse is a Perl module to parse required modules from Makefile.PL. SYNOPSIS use Module::MakefilePL::Parse; open $fh, Makefile.PL; $parser = Module::MakefilePL::Parse->new( join("", ) ); $info = $parser->required;...
8.19 KB  
Libraries  -  sqlpp 0.06
sqlpp Perl package is a SQL preprocessor. sqlpp is a conventional cpp-alike preprocessor taught to understand SQL ( PgSQL, in particular) syntax specificities. In addition to the standard #define/#ifdef/#else/#endif cohort, provides also...
10.24 KB