Caucho Forums  

This forum is permanently closed because of spam. For free community support, please visit Google Groups:


Go Back   Caucho Forums > Quercus

Reply
 
Thread Tools Display Modes
  #1  
Old 08-02-2013, 04:05 PM
shabb shabb is offline
Junior Member
 
Join Date: Aug 2013
Posts: 1
Default Threads, Futures, ForkJoins in Quercus

Its exciting to see the possibility to run PHP on the JVM mainly due to what the JVM can offer. However I am struggling with a few concepts.

My first question is how are Java Class Methods override? In this threading example the Runnable object is accepted, but the run method is never executed. Is this because in Quercus you cannot override methods?

Code:
<?php
//-----------------------------------------------------------------------------
//-- Thread Example
//-----------------------------------------------------------------------------
import java.lang.Runnable;
import java.lang.Thread;
import java.lang.System;
import java.lang.InterruptedException;

function threadMessage($message) {
	$threadName = Thread::currentThread()->getName();
	printf("%s: %s<br />\n", $threadName, $message);
}

class MessageLoop implements Runnable {
	public function run() {
		threadMessage("Running?");
		$importantInfo = array(
			"Mares eat oats",
			"Does eat oats",
			"Little lambs eat ivy",
			"A kid will eat ivy too"
		);

		try {
			for ($i = 0; $i < count($importantInfo); $i++) {
				// Pause for 4 seconds
				Thread::sleep(4000);
				// Print a message
				threadMessage($importantInfo[$i]);

			}
		} catch (InterruptedException $e) {
			threadMessage("I wasn't done!");

		}
	}
}

threadMessage("Starting MessageLoop thread");

$startTime = System::currentTimeMillis();
$ml = new MessageLoop();
$t = new Thread($ml);
$t->start();

threadMessage("Waiting for MessageLoop thread to finish");

// loop until MessageLoop
// thread exits
while ($t->isAlive()) {
	threadMessage("Still waiting...");
	// Wait maximum of 1 second

	// for MessageLoop thread
	// to finish.
	$t->join(1000);
	threadMessage("Done?");

	if (((System::currentTimeMillis() - startTime) > patience) && $t->isAlive()) {
		threadMessage("Tired of waiting!");

		$t->interrupt();
		// Shouldn't be long now
		// -- wait indefinitely

		$t->join();
	}
}
threadMessage("Finally!");
?>
My next question is a problem I noticed with a warning:

Code:
Warning: 'ObjectExtValue@102719509[MessageLoopCallable]' of type 'ObjectExtValue' is an unexpected argument, expected Callable
If I implement a Java interface is there a way to cast it as a java object and not ObjectExtValue? I guess this falls into generics as well?

Code:
<?php
//-----------------------------------------------------------------------------
//-- Concurrency Example (Threads)
//-----------------------------------------------------------------------------
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.lang.InterruptedException;
import java.lang.Long;

class MessageLoopCallable implements Callable {
	public function call() {
		$importantInfo = array(
			"Mares eat oats",
			"Does eat oats",
			"Little lambs eat ivy",
			"A kid will eat ivy too"
		);

		for ($i = 0; $i < count($importantInfo); $i++) {
			// Pause for 4 seconds
			// Print a message
			echo $importantInfo[$i];
		}
	}
}

$mlc = new MessageLoopCallable();

$executor = Executors::newFixedThreadPool(2);
$future = new FutureTask($mlc);
$executor->execute($future);
$executor->shutdown();

try {
	$executor->awaitTermination(Long::MAX_VALUE, TimeUnit::NANOSECONDS);
} catch (InterruptedException $e) {
  // interrupted
}
?>
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:12 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.