|
Not a specific code question When are the best times to synchronize? It seems like the logical choice is for when transactions occur (adding / modifying database or repository). But are there other times when synchronization becomes crucial? Or should it be kept to the minimum?
|
# ? Jan 15, 2010 05:41 |
|
|
# ? Apr 26, 2024 18:54 |
|
geeves posted:Not a specific code question If you are modifying a database or repository it is better to let the transaction engine of the database to do the work of synchronizing. Synchronizing mostly becomes an issue when one thread needs to pass or modify data that another thread can use. You try to keep it to a minimum by only locking what's absolutely needed.
|
# ? Jan 15, 2010 07:52 |
|
geeves posted:Not a specific code question This is a very complex question, one that could easily span a graduate level course. Start reading up, I like the book "Java Concurrency in Practice".
|
# ? Jan 15, 2010 07:57 |
|
geeves posted:Not a specific code question The synchronization locking must not be more fine-grained than the represented data. code:
The getBalance() method does not cause any change to the value of balance, and because the object assignment that occurs in deposit() and withdraw() is atomic, getBalance() does not need to be synchronized. The race condition that could invalidate the balance occurs because in deposit() and withdraw() we are both checking or getting the value of balance before using it to calculate the new balance. If one thread is processing the bytecodes for deposit() or withdraw(), and another thread is doing the same simultaneously owing to thread preemption or parallel processing, the values on the stack for balance that the first thread sees would be wrong, and the resulting value would be wrong. E.g., It can work sometimes if the sequence of operations performed by the JVM is "just right" resulting in the correct value of $16 for balance:
code:
This example assumes that all of the values are stored on the JVM stack and read from a single copy of the data in the JVM's memory, but the JVM memory model also allows each thread to have its own copy of parts of memory, so it gets even worse unless you syncrhonize. In addition to getting a lock on an object, synchronization causes the thread to flush its own copy of memory and read from the "real" memory. edit: spelling and grammar Fly fucked around with this message at 17:26 on Jan 15, 2010 |
# ? Jan 15, 2010 17:21 |
|
...
maskenfreiheit fucked around with this message at 03:44 on Sep 29, 2010 |
# ? Jan 17, 2010 03:07 |
|
GregNorc posted:
literals like this are doubles by default, unless you append an 'f' as in -123.987f.
|
# ? Jan 17, 2010 03:40 |
|
Is there a design patterns megathread?
|
# ? Jan 20, 2010 16:26 |
|
mister_gosh posted:Is there a design patterns megathread? You tell me. http://forums.somethingawful.com/f/search
|
# ? Jan 21, 2010 00:58 |
|
This is a strangely specific JSTL question. I'm using forEach to loop through some data that looks like this Program 1 | Homeless Shelter Program 2 | Homeless Shelter Program 3 | Soup Kitchen Program 4 | Soup Kitchen Right now I want to write a header whenever the program type (second column) changes. To do this I'm creating a temporary variable, populating it with the program type of the current row at the end of the loop, and then I have some logic at the start of the loop that compares the program type of the current row and the temporary variable and throws up a header when its different. I feel like there is a better way to do this but I'm coming up short searching around. Any ideas?
|
# ? Jan 21, 2010 20:50 |
|
Never mind, got it with this little line. Friday afternoon coding code:
|
# ? Jan 21, 2010 22:00 |
|
GregNorc posted:So I have this really simple code I need to run. There seems to be a syntax issue with the negative number, but I'm not sure how to write it... do I put it in quotes or something? In Java you have to put an 'f' after float values. So change the line where you assign x to : code:
|
# ? Jan 22, 2010 03:32 |
|
Is there a way to listen for keyboard events in a console app? It's fairly easy when using KeyListeners, but it looks like I can only use them on GUI apps.
|
# ? Jan 23, 2010 03:02 |
|
CoasterMaster posted:Is there a way to listen for keyboard events in a console app? It's fairly easy when using KeyListeners, but it looks like I can only use them on GUI apps. I'm not sure of anything Java-specific, but to do that in other languages, you'd use some sort of terminal control library like (n)curses. A quick google search suggests that there is a Java curses library, so you could try that.
|
# ? Jan 23, 2010 04:49 |
|
I guess you could read directly from the console input by doing System.setIn(yourInputStream);
|
# ? Jan 23, 2010 11:57 |
|
Can anyone tell me why this:code:
[C@3e25a5[C@19821f[C@addbf1[C@42e816[C@9304b1[C@190d11[C@a90653[C@de6ced[C@c17164 as the output? The intent of the code ought to be clear - .fillBoard takes a 117 character length string and populates it into the 9x13 board. .printBoard takes the board and should print out a string that it makes... lewi fucked around with this message at 01:39 on Jan 26, 2010 |
# ? Jan 24, 2010 23:59 |
|
Java is not C. toString() on a char array in Java works exactly like toString() on any other array type: it produces some internal information about the array reference. You probably want to just use Strings.
|
# ? Jan 25, 2010 00:14 |
|
Lewi: If you want to print out a nicely formatted version of a primitive array, use Arrays.toString(foo).
|
# ? Jan 25, 2010 00:17 |
|
rjmccall posted:Java is not C. toString() on a char array in Java works exactly like toString() on any other array type: it produces some internal information about the array reference. You probably want to just use Strings. I was working from this bit from the java String documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html posted:Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example: This seems to imply that you can cast a char array to a string... Internet Janitor posted:Lewi: If you want to print out a nicely formatted version of a primitive array, use Arrays.toString(foo). lewi fucked around with this message at 00:27 on Jan 25, 2010 |
# ? Jan 25, 2010 00:18 |
|
lewi posted:This seems to imply that you can cast a char array to a string... There's a big difference between creating an object from a character array and casting the array itself to an instance of that object. As rjmccall said, Java is not C- object instances should not be thought of as structs. Also note that the Javadocs for String(char[]) indicate that the source array is copied. If it was possible to create a string backed by a character array that could then be modified and reflected in the associated string, it wouldn't be immutable.
|
# ? Jan 25, 2010 00:29 |
|
I'm obviously not as awake as I thought I was - I was misunderstanding the difference between String(char[]) and toString(). drat. Thanks for your help guys.
|
# ? Jan 25, 2010 01:12 |
|
...
maskenfreiheit fucked around with this message at 03:39 on Sep 29, 2010 |
# ? Jan 25, 2010 17:09 |
|
One thing that jumps out at me is that all of your if.. blocks are not wrapped in curly braces: {}. In Java, it is syntactically OK to have an if.. block with no braces, but that means only the next line is part of the if.. block.
|
# ? Jan 25, 2010 17:21 |
|
look here for the error:code:
e: yea, forgot to mention that, your style is horrible. Stay way from bracket-less statements lamentable dustman fucked around with this message at 17:28 on Jan 25, 2010 |
# ? Jan 25, 2010 17:23 |
|
GregNorc posted:Our professor just tells us to talk to the TA if we have questions, and our TA deducts points if we ask her questions about the lab, so I'm not really sure where to turn.
|
# ? Jan 25, 2010 18:06 |
|
...
maskenfreiheit fucked around with this message at 03:44 on Sep 29, 2010 |
# ? Jan 25, 2010 18:50 |
|
The problem has nothing to do with variable scope. I will advise you the same way I advise every intro student: stop abstractly reasoning about your code. Don't get me wrong abstract reasoning is a very important skill! but it's easy to blind yourself to bugs, thinking that the code is doing what you want instead of what you've written. It's very simple. Get out a piece of paper. Pretend you're the computer. Whenever a variable comes into scope, write down the name of the variable and its current value. Whenever you assign to a variable, cross out the old value and write down the new value. Don't skip steps; just follow the program, line by line, expression by expression, and I guarantee that you will find your bug.
|
# ? Jan 25, 2010 19:24 |
|
GregNorc posted:OK so I'm guessing it's something relating to scope? (I was getting 4 for the first test sin ce 45/10 = 4.5 and since the result is an int, it gets truncated to 4, hence the appearance it was working?) You are thinking to far ahead. Focus on one error at a time. In the iteration of the code you posted the biggest error in the code is the infinite loop that is causing the "hanging." Follow the debugging steps rjmccall posted and remember to take things one at a time. Using sys outs or a debugger to pinpoint your problems couldn't hurt either.
|
# ? Jan 25, 2010 19:40 |
|
Mustach posted:
Seconding this. That's majorly hosed. You should be encouraged to ask as many questions as necessary to understand the lesson/exercise.
|
# ? Jan 25, 2010 19:50 |
|
I'm being told that my code is crap unless I'm implementing design patterns that promote an efficient design. I'm self taught and have only had the opportunity to have one "mentor" (1 year) for the past ten year career (and that was in Perl). Most of the code I've needed to manage was small projects and thus wrapping my head around some efficient reusable designs is a little tough. I have a "node" object that I would like to represent on the client side, middleware or in a UI as a Data Transfer Object/Value Object. I have the potential here for both the facade and/or the factory patterns to manage the various types of "node" objects, let's call them StandardNode, ComplexNode1 and ComplexNode2. The client side should request the DTO and then I imagine a DAO class will take care of the dirty work of getting the necessary information through a proprietary API, executing the appropriate setters on the DTO and then handing that DTO back to the client side. The client side could then access that DTO for information or fire off more requests to the factory to get the nodes next child or some other entirely different node. I'm really not sure how to implement this. I figure the facade is possible here because it would hide the potential complexities of the different node types from the uncaring client, but then the factory is also possible here because it could manage the creation better of the DTO. I'm just lost and could either use some pseudo code to help paint a clearer picture, or at the very least be pointed in the direction of a definitive (online) source, outside of the GoF, that would help me quickly get up to speed.
|
# ? Jan 25, 2010 21:28 |
|
Question about generics and inheritance. I have a domain model hierarchy like so: Persistable > Address Persistable as a parent has fields that all persistable objects have, like id, timeCreated, timeModified. Address is a subclass which has properties such as address, city, state, yada yada. I have a Dao class which allows for dynamic where clauses. Traditionally, I just used static strings to identify columns, like so: code:
code:
code:
code:
FateFree fucked around with this message at 00:21 on Jan 26, 2010 |
# ? Jan 25, 2010 23:09 |
|
FateFree posted:Recently we upgraded to Java 1.5, so I wanted to add type safety. At first I looked into enums for these fields, but I didn't like the fact that I couldn't extend the enums of the Persistable class. So instead, I created a Property<T, V> class, where T is the type of the class, and V is the type of the value. An excellent idea! Surely your work is touched with genius. FateFree posted:Does anyone have any idea how I can make this work while maintaining the inheritance of my common Properties? Its almost like I need for the find method to accept T OR a parent of T. If only this had been a crucial component of the original idea! It is unfortunate you were unable to consult these fora previously. code:
rjmccall fucked around with this message at 23:52 on Jan 25, 2010 |
# ? Jan 25, 2010 23:44 |
|
rjmccall posted:An excellent idea! Surely your work is touched with genius. Well thanks, I tried to use the super property but apparently I wasn't using it correctly. Your way solved my problem. Was there something wrong with my original Property<T, V> idea in general for the sarcastic genius remark?
|
# ? Jan 26, 2010 00:02 |
|
FateFree posted:Was there something wrong with my original Property<T, V> idea in general for the sarcastic genius remark? Nope; it's just that it's pretty much exactly what I told you to do twenty days ago when you asked this question then.
|
# ? Jan 26, 2010 00:18 |
|
rjmccall posted:Nope; it's just that it's pretty much exactly what I told you to do twenty days ago when you asked this question then. At touche salesman. I editted my last question, since I asked on a couple forums I didn't remember that I asked here. But works great now, thank you.
|
# ? Jan 26, 2010 00:23 |
|
Edit: Double Post
maskenfreiheit fucked around with this message at 21:31 on Mar 13, 2017 |
# ? Jan 26, 2010 02:08 |
|
How do I use a Singleton on a subclass of a abstract class? NewConnection.java code:
code:
code:
|
# ? Jan 26, 2010 18:04 |
|
code:
|
# ? Jan 26, 2010 19:03 |
|
MEAT TREAT posted:
Thanks! I should have been more specific though. Test.java does not necessarily know which Connection class was used. I suppose I could set a property somewhere in the application which defines the connection type, and then execute a case switch, if/else scheme. code:
|
# ? Jan 26, 2010 20:46 |
|
mister_gosh posted:I could do this, but it seems like bad design. You could move that to a method in some connection factory or whatever. So, something along the lines of: code:
RitualConfuser fucked around with this message at 05:29 on Jan 27, 2010 |
# ? Jan 27, 2010 05:21 |
|
|
# ? Apr 26, 2024 18:54 |
|
This code is under the presumption that you're looking for there to be many possible implementations of the abstract class but want only one at runtime. Using System.getProperty() isn't necessary but fits with established practice in the libraries. In order to make this work, you'll also need to make sure that you've got a META-INF/services directory in the JAR file with the children of the Connection class listed.code:
|
# ? Jan 27, 2010 05:24 |