I have situation where a user can request java server to send a value to an embedded device, and if the device is asleep, that value needs to be stored in a queue until the device wakes up and sends a position to java server, at which point the server checks if there is a value in the queue and if there is, it then sends that value to the device. The maximum size of the queue is 1 for now. And if the user makes continuous requests to java server, the old value is removed and the new value is added.
Initially I was looking into BlockingQueue, but the problem with that is, well, it blocks. queue.put(value) will block if queue is full, and queue.take() will block if queue is empty. I can't have something that blocks. When the device responds to server, server checks if value is in queue, if it is not then the server carries on the rest of its responsibility. Thus, I then entertained ConcurrentLinkedQueue. While queue.offer(value) and queue.poll(); allow you to add and remove values respectively from the queue without blocking, it does not allow you to set a maximum size limit of the queue. My queue must have a maximum size limit and it has to always be the newest value the user submits to the queue (where old values are removed).So this is what I came up with:
Java Code: class Unit {
private List<Integer> cmdQueue;
public Unit(){
cmdQueue = Collections.synchronizedList(new LinkedList<Integer>());
[code]....
I use Collections.synchronizedList, as I do here, do I still need to use synchronize as I did above?
I have a multi-threaded application but one function (A) I need to call, that is provided by a third party, is not thread safe. I need to make parallel calls to function (A) so my only option is to start multiple process that I can call from each of my main applications threads.
To do this I created a RMI interface between a the client and the remote process. On the server side I start multiple processes each assigned a different port. On the client side I have a queue manager that connects to the processes I started and builds a queue of them. It passes the client to the threads as needed so they can function in parallel.
I am new to Java development and have the following questions:
1. Each of the server processes require an init function to be run before providing the service. How do I setup the server processes to continue to run in memory listening for a a service request after running the init processes.
2. Is there a better implementation of what I am trying to do?
I read JEE6 doc and confused with : Does container managed entity manager (injected by PersistenceContext annotation) is thread-safe in stateless session bean in multiple-thread env?
See code below, if there are 2 requests to stateless sesion bean in 2 concurrent threads , is it using same Entity Manager Instance or not?
@Stateless(name = "HRFacade", mappedName = "HR_FACES_EJB_JPA-HRFacade-HRFacade") public class HRFacadeBean implements HRFacade, HRFacadeLocal { @Resource SessionContext sessionContext;
I'm currently learning about Swing but I can't get my head round this piece of the code. Here is a simplified gui (not interested in the gui part but the execution)
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { new SwingDemo(); }
I get the above, create a new instance of SwingDemo in the main thread which starts up the gui through the constructor. However, then the tutorial says that I should avoid doing the above but do this instead:
public class SwingDemo implements ActionListener { SwingDemo(){ JFrame jfrm = new JFrame("Simple gui pro"); //rest of code public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { //why do this instead? public void run(){ new SwingDemo(); } }); } }
Reading, it talks about an event-dispatching thread which has completely lost me... Why not just instantiate the object directly instead of creating another thread?
im having an issue with the 3rd thread that are supposed to merge the two sorted sub arrays , i pass the 2 subarrays to my runnable function sortlist and they are renamed IntSortList 1 and 2 and th1.start() and th1.join() are called and it works fine, but then i have another runnable constructor that takes IntSortList 1 and 2 but it does take a runnable. below is the code in my main,
Runnable InSortlist1 = new sortList(data2p1); Runnable InSortlist1 = new sortList(data2p1); Thread th1 = new Thread (IntSortlist1); Thread th2 = new Thread (IntSortlist2); try { th1.start(); th1.join();
Is it possible to define the same message driven bean as a listener to different queues?
The goal being to define different redelivery configuration for different kind of messages, but handle them all through a single deployment MDB for unity/entity purposes.
The other option is to have many MDBs targeted to the same source code.
Ok, so I'm making a game with a space ship that flies around the universe and discovering new planets. It works fine so far, but I'm looking to make it perform better and be better compatible with lesser-processors.So, I'm trying to put in an FPS counter and an entirely new game loop so that my game can decide for me what FPS I should use.
I have two classes. Heres the big, main one: [Java] package cyentw.game.src; import java.awt.Color; import java.awt.Font; impor - Pastebin.com *I want to change the loop and put an FPS counter in around line 456, you can scroll past the rest if you'd like*And heres the init frame one, in case you'd like to see it for some reason.
Java Code:
package cyentw.game.src; import javax.swing.JFrame; public class Start extends JFrame{ public static JFrame frame; public static int WIDTH = 500; public static int HEIGHT = 500; public Start() {
[code]....
how to make my game loop as quickly (or a bit slower) as it can, and my FPS is static.
leaveQ method does not work..To see the other files related to these code click here:(Its a dropbox location) URL....Java Code:
public class CustomerQ { private int MaxLength; private int totalCustomers;//assuming #of customers served int Qlength; Customer cus; LinkedList4Q cus4Q;
I am doing a bank queue simulator program which will figure what will happen in 100 minute where 0 to 2 customers comes every minute. 3 counters will handle these customers each counter will poll the customer after 3 minutes.my problem is queue.poll()is not working in counter method and it is adding null values to the queue in the same method. when i add while customers.isEmpty(); the program will not work i do not know why
package dataalgo; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.Random; public class customer {
import java.util.*; public class DoubleEndedQueueImplHW22 { ArrayList<Integer> deque = new ArrayList<Integer>(); public void insertFront(int a){ System.out.println("adding at front: "+a); deque.add(0,a); System.out.println(deque);
The following codes shows an implementation of an enqueue function of a FIFO immutable queue, and the output result shows the String "c".
I don't understand why since I expected it should be null.
The head and the tail of an ImmutableQueue Object are two separate queue, and each time I call the enqueue function, it just return a new object with a new tail, however, the head is not modified except the first two times I call the function.
Therefore, I expected head.next.next should be a null element, but the result is not like that.
public class ImmutableQueue<E> { private int size =0; public Queue<E> head; public Queue<E> tail; public ImmutableQueue(){} private ImmutableQueue(Queue<E> hd, Queue<E> tl){ head=hd; tail=tl;
I have been trying to learn how to use the TabbedPane GUI. I can get the tabs to show up, but the buttons I have placed in each tab do not show up. Why this is not working. I assume that, for some reason, the buttons are not linking with their respective panels, or the panels are not linking to the respective tabs.
public class Someone { String name; int age; ArrayList <Someone> listarr = new ArrayList <Someone>(); public Someone(String name1, int age1) { name = name1; age = age1;
[code]...
1. In the compare method, what happens when it returns one of the 0, -1, or 1? How does returning such values contribute to the sorting process? How does it take in information (like I know it does through the parameters, but how exactly)?
I am supposed to implement a custom Map interface and I'm having some trouble with this method:
// 1. From the interface /** * Gives an iterator iterating over the key set from the smallest key that is not less than the key provided. * @param key the key * @return the iterator * @throws NullPointerException if key == null */
public Iterator<Key> tailIterator(Key key);
[Code] .....
My implementation is wrong according to a JUnit test. Also, to get a full score this method should have a worst case running time of O(log N), which obviously isn't the case now. (My Map implementation is currently based on binary search in an ordered array, keeping a prallel array for the values).
I have always wanted to program A*, Breath First Search and Depth First Search ever since I took AI. I will only post BFS at the moment but it is only searching the row.
Java Code:
//Breath First Search. The first node passed to it is the start button void bfs(Mybuttons button){ Queue<Mybuttons> q = new LinkedList<Mybuttons>(); q.add(button); button.setIcon(button.setButtonToBlueWhileSearched()); button.model.visted=true;
[Code] ....
Output:
Java Code: In while loop In get Child Node In get Child Node In get Child Node In get Child Node In while loop In get Child Node In get Child Node In get Child Node In while loop In get Child Node In get Child Node In while loop In get Child Node mh_sh_highlight_all('java');
Say you have a bunch of animals, `Rat` `Cat` `Dog` and `Fish`, and you want to serialize/deserialize these animals. Each has its own `serialize()` method. That all works fine and good, if you want to save your Cat to a file, you call `Cat.serialize()` and then you write it to a file.
But what about deserializing? Sure, you could save an extra bit of data that states the type of animal, then use reflection to get a static method (say, `deserialize(String serialized)`) and then invoke it. But that's not safe. What if someone creates a new animal 'Rabbit' and doesn't include a `deserialize(String serialized)` method? Sure, you could then just catch the error and ignore the issue, but that seems unclean and bad practice.
You could also create an enum. Each item in the enum must have a `deserialize(String serialized)` method, and the aforementioned piece of data that states the type references the name of its enum item. Problem is, not very adaptable. Enums don't allow for other animals to be added at runtime.
The way I have been solving this issue is mapping the name of the objects against a deserializer object. Basically each animal would have to 'register' itself by entering it's name and deserializer (implements `Deserializer` interface) object into a HashMap. The deserializer object can then be retrieved via the name of the animal at a later time.
My question is, what is the best method to go about implementing deserialization? Also, is the method I have been using good/bad practice?
Finally, if you are wondering why animals would be added at runtime, the reason is because animals might be added by other non accessible parts of the program, or by things such as plugins.
Example of the method I have been using is below.
Animal Interface
Java Code:
public class Animal{ public String serialize(); } mh_sh_highlight_all('java'); Deserializer Interface
[code]....
And when you needed to deserialize:
Java Code:
Deserializer d=AnimalSpecies.getSpeciesDeserializer(serialized.split(AnimalSpecies.DELIMITER)[0]); if (d!=null){ d.deserialize(serialized); } mh_sh_highlight_all('java');
What I mean by this is that any serialized Animal must have a way of identifying which animal it is so that it's deserializer can be accessed. This can be worked around by implementing a wrapper that adds this information directly before it is written to a file, and removes it directly before deserialization.
Why not use built in serialization/deserialization? I would like the serialized data to be readable and easily editable.
in my class is implement a heap and use some of the methods we were provided. The methods I was provided to code and use are "siftDown", "isEmpty" and "heapify". I'm pretty sure the code I have written for "heapify" and "isEmpty" is correct, where I think I am finding fault is the code for my "siftDown". Would you mind taking a look at my code and see why when adding integers to the heap object that I have created in the main code, that they are not being output correctly?
public class Tester { public static void main(String[] args) { Heap myHeap = new Heap(); myHeap.insert(9); myHeap.insert(15); myHeap.insert(6); myHeap.insert(4); myHeap.insert(10); myHeap.insert(9); myHeap.insert(3);
I am building a frogger game and am having some trouble implementing collisions. I've been going off of various different tutorials and it seems like my frogCollisions function should work based off them, but no matter where I place it collisions aren't registering. Here is the frogCollisions function I am trying to use. I don't think it is very efficient to be checking all of the items in my manager class every loop, but can't think of another way to do this with manager classes. You can see my full code on my github if you would like to see the whole project. It's at: URL....
public void frogCollisions() { for(int i = 0; i < carManager.smSize(); i++) { if(f.intersects(carManager.smCars.get(i).getRect())){ player.decreaseLives(); System.out.println(player.getLives()); checkGameOver(); // NEED TO IMPLEMENT STILL
I tried to write a program to implement a stack but it has a bug I am unable to solve.The bug is that whenever I choose an operation to perform, eg push, After performing the operation, the loop is executed once again and Invalid choice message appears, i.e. the default case. And then the loop again executes to choose further option. Here is my code
class Stack { private char[] stck; private int len; private int top;