Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Ron McLeod
paul wheaton
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Himai Minh
Bartenders:
Forums:
Java in General
Threads and Synchronization
Using CyclicBarrier
Bob Matthews
Rancher
Posts: 688
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is my first attempt at RMI code
Don't know whether it is part of my particular solution I am seeking ?
Writing the RMI interface
import java.rmi.*; // // OptimizedTrades Interface // // Interface for an RMI service that optimizes a list of Prospective Trades // public interface OptimizedTrades extends java.rmi.Remote { // Calculate the list of optimized trades public OptimizedTrades optimize ( String ProspectiveTrades ) throws RemoteException; }
Server code:-
import java.math.*; import java.rmi.*; import java.rmi.server.*; // // OptimizedTradesServer // // Server for a RMI service that optimizes a list of Prospective Trades // public class OptimizedTradesServer extends UnicastRemoteObject implements OptimizedTrades { public OptimizedTrades () throws RemoteException { super(); } // Calculate the list of optimized trades ** TODO ** return (OptimizedTrades); // Assign a security manager, in the event that dynamic // classes are loaded if (System.getSecurityManager() == null) System.setSecurityManager ( new RMISecurityManager() ); // Create an instance of our Optimized Trades service server ... OptimizedTradesServer svr = new OptimizedTradesServer(); // ... and bind it with the RMI Registry Naming.bind ("OptimizedTrades", svr); System.out.println ("Service bound...."); }
Client code:
import java.math.*; import java.rmi.*; import java.rmi.server.*; // // OptimizedTradesServer // // Server for a RMI service that optimizes a list of Prospective Trades // public class OptimizedTradesServer extends UnicastRemoteObject implements OptimizedTrades { public OptimizedTradeServer () throws RemoteException { super(); } // Calculate the list of optimized trades ** TODO ** return (OptimizedTrades); // Assign a security manager, in the event that dynamic // classes are loaded if (System.getSecurityManager() == null) System.setSecurityManager ( new RMISecurityManager() ); // Create an instance of our optimized trades server ... OptimizedTradesServer svr = new OptimizedTradesServer(); // ... and bind it with the RMI Registry Naming.bind ("OptimizedTrades", svr); System.out.println ("Service bound...."); } import java.rmi.*; import java.rmi.Naming; import java.io.*; // // // OptimizedTradesClient // // public class OptimizedTradesClient { // Check for hostname argument if (args.length != 1) { System.out.println ("Syntax - OptimizedTradesClient host"); System.exit(1); } // Assign security manager if (System.getSecurityManager() == null) { System.setSecurityManager (new RMISecurityManager()); } // Call registry for OptimizedTrades OptimizedTrades service = (OptimizedTrades) Naming.lookup ("rmi://" + args[0] + "/OptimizedTrades"); DataInputStream din = new DataInputStream (System.in); for (;;) { System.out.println ("1 - Calculate list of optimized trades"); System.out.println ("2 - Exit"); System.out.println(); System.out.print ("Choice : "); String line = din.readLine(); Integer choice = new Integer(line); int value = choice.intValue(); switch (value) { case 1: System.out.print ("List of optimized trades: "); line = din.readLine(); System.out.println(); choice = new String (line); value = choice.intValue(); // Call remote method System.out.println ("Answer : " + service.OptimizedTrades); break; case 2: System.exit(0); default : System.out.println ("Invalid option"); break; } } }
Bob Matthews
Rancher
Posts: 688
posted 9 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can I modify the following example code for my first of two 'waits' where MAXQUEUE = no. of subscribed clients, each of which will send a message to the server with a prospective trade ?
import java.util.Vector; class Producer extends Thread { static final int MAXQUEUE = 5; private Vector messages = new Vector(); @Override public void run() { try { while (true) { putMessage(); //sleep(5000); } } catch (InterruptedException e) { } } private synchronized void putMessage() throws InterruptedException { while (messages.size() == MAXQUEUE) { wait(); } messages.addElement(new java.util.Date().toString()); System.out.println("put message"); notify(); //Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. } // Called by Consumer public synchronized String getMessage() throws InterruptedException { notify(); while (messages.size() == 0) { wait();//By executing wait() from a synchronized block, a thread gives up its hold on the lock and goes to sleep. } String message = (String) messages.firstElement(); messages.removeElement(message); return message; } } class Consumer extends Thread { Producer producer; Consumer(Producer p) { producer = p; } @Override public void run() { try { while (true) { String message = producer.getMessage(); System.out.println("Got message: " + message); //sleep(200); } } catch (InterruptedException e) { e.printStackTrace(); } } public static void main(String args[]) { Producer producer = new Producer(); producer.start(); new Consumer(producer).start(); } }
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
◄
1
2
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
wait until variable has reached specific value
Join v/s CountDownLatch/CyclicBarrier
HorseRace example from Thinking in Java
Unsure about thread algorithm
lerning debug
More...