Over a million developers have joined DZone.

How to Cluster an Android Phone and a PC Using JavaX

Yes, you can cluster an Android phone and a PC using JavaX! Stefan Reich shows us this quick tutorial on how to do so. It's easier than you think.

· Mobile Zone

Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud, brought to you in partnership with IBM.

Remember the old days of the Commodore 64? It had a 1 MHz CPU which could run up to half an instruction per clock. It also had a disk drive which contained the exact same CPU.

Usually, the drive's processor didn't do much except when loading and saving. But some smart people actually turned the computer + drive combo into a sort of cluster, calculating Mandelbrot sets on both machines in parallel.

Today, you can do a similar thing with your PC and your smartphone — it’s got a processor too. Typically, today, even a multi-core.

But normally you can’t access the phone’s processor directly. It will only run its predefined apps.

Don’t worry: Once again the miracle language will help— JavaX will make your PC and your phone act as a cluster.

Let’s check it out. All you need is the app on the phone and this program on the PC.

Connect the phone to the PC with a USB cable. Start the app on the phone and hit the “Start Awareness” button. It will make the phone “aware”, i.e. able to receive commands.

Also, sadly, because Android is a very limited platform, you need to convince it to even allow communication between the devices. You do this by either turning on “USB tethering” on the phone,or by installing “adb” on the PC (the Android debugger). Either way should be okay, JavaX will try to make it work.

Now we’re good to go. Start the PC program and you’ll have our fancy Java interpreter prompt ready for you. You can now, with one function call (“quickPhoneEval”), run JavaX code on the phone! This is what the program looks like:

It can run Java code on the fly with the "!j" command. So a simple test line for our purposes would be:

!j quickPhoneEval("1+2")

…which will indeed return 3 as expected. We can verify that this actually ran on the phone like so:

!j quickPhoneEval([[System.getProperty("java.vendor")]])

…which, on my device, says “The Android Project”.

This is the more complicated command from the screenshot:

!j quickPhoneEval([[l(listFiles(new File(androidHome(), ".javax")))]])

This will show how many temp directories JavaX has created on the Android phone.

(The [[ ]] brackets are multi-line string literals in JavaX. I borrowed this syntax from Lua.)

This looks simple, right? And it is. Notice also how we can use any JavaX standard function just by calling its name.

What goes on behind the scene is not quite so simple. Java code is made, compiled (to .class/.dex) and run, both on the PC and on the phone, and objects are wrapped and unwrapped automatically when transferring them between the devices. When all is done, the phone cleans up all the temporary classes it made.

You can even make a List or a Map on the phone and further process it on the PC.

For another example, check out this program which makes the PC and the phone speak at the same time (with different voices).

You know what? We should start calculation competitions. My phone has got a quad-core processor, but I am doubtful it will beat the PC’s single core CPU, even with parallelization. But do I know for sure? No, I didn’t run any numbers yet. Let’s make a benchmark and see how they stack up! It’s all a one-liner in JavaX…

The Mobile Zone is brought to you in partnership with Strongloop and IBM.  Visually compose APIs with easy-to-use tooling. Learn how IBM API Connect provides near-universal access to data and services both on-premises and in the cloud.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}