|
FateFree posted:If I'm making a generic class <T extends BaseObject, V> how can I enforce that V is a long? If I say <V extends Long> I get an ugly warning about not being able to extend final classes, even though it seems to accomplish what I want. Is there any other way?
|
# ? Jun 16, 2010 18:43 |
|
|
# ? May 9, 2024 07:05 |
|
Don't you want class <T extends BaseObject, Long> ? edit: Whoops, no, this doesn't make sense epswing fucked around with this message at 23:13 on Jun 16, 2010 |
# ? Jun 16, 2010 18:44 |
|
epswing posted:Don't you want class <T extends BaseObject, Long> ? That's not quite what Mustach is saying... He's saying you don't need a type parameter at all for V, just: code:
|
# ? Jun 16, 2010 21:57 |
|
You would enforce that the second type parameter is a long if you were extending a class that had two generic type parameters and the subclass required that one be a Long. Forgive me if this isn't correct syntax: code:
|
# ? Jun 16, 2010 22:09 |
|
Yes this turned out to be the answer. I got caught up in generics insanity before I realized it. Thanks. Now for something a little harder. I am using EhCache (thread safe cache, based on ConcurrentHashMap) to store a counter (AtomicInteger). The counter is basically the count of all rows in the database for some query. I'd like to pro-actively manage this value to reduce the number of queries, so I have code like so, that I will call any time someone saves a new object to the database: code:
My question is, is this thread safe? I know the AtomicInteger is a synchronized increment operation, and I know that ehcache is thread safe, but I don't know if this particular block of code is safe to use together. I think it is because of AtomicInteger, and if it was a regular int with counter++ it would be unsafe. Still I'd feel better with someone elses opinion.
|
# ? Jun 16, 2010 23:03 |
|
What's the deal with the null check here --- a potential race here would be an another thread inserting something in the cache between the get and the check. Would that be a problem?
|
# ? Jun 16, 2010 23:30 |
|
Well lets see, there may not be a count in cache yet, and I would not have the information to add one. All I know is on a new save, its count++ and a delete its count--, the actual number requires a query (whoever is requesting the count will first check cache, if not there, queury and place it in cache). But if another thread inserted something after my get, it means they will be putting the most up to date number in cache since its coming directly from the query. So i believe in that sense it is safe.
|
# ? Jun 17, 2010 00:35 |
|
So as long as you can guarantee that you never replace the object in the cache with a new one, then yes your code is thread safe. However you still could have a race condition where a thread is reading that AtomicInteger at the same time as another is null-checking right before an update. If you need an ALWAYS accurate row count you should probably either synchronize all reads and writes or just run the query and see if it'll really cause a performance issue.
|
# ? Jun 17, 2010 01:39 |
|
FateFree posted:But if another thread inserted something after my get, it means they will be putting the most up to date number in cache since its coming directly from the query. So i believe in that sense it is safe. Consider the following scenario: Thread 1: Try to save a record that affects key "query1" Thread 1: Successfully update DB Thread 2: Call get() or whatever to get rowcount for "query1" Thread 2: No AtomicInteger found, so fetch correct count from the DB, which should be 1 Thread 2: Insert entry for "query1" with new AtomicInteger(1) Thread 2: Finished Thread 1: Lookup entry for "query1" in your map, come back with AtomicInteger with value of 1 Thread 1: Remember, initiated with save command, so increment this integer ERROR: "query1" now has a rowcount of 2. In my experience, you will drive yourself nuts trying to keep these 2 pieces of information, the rowcount in RAM and the rowcount in the DB, in sync. Anything that needs to know the rowcount for this query should probably just fetch it on demand.
|
# ? Jun 17, 2010 02:09 |
|
Just when you think youve got something figured out haha. Thank you sir. I suppose the best solution is to maintain the count in cache, and just clear it on save or delete operations. I guess there is no way to ever guarantee an accurate count, even if it was never cached another thread could have saved a value immediately after I retrieved mine.
|
# ? Jun 17, 2010 02:27 |
|
http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent(K, V) if you used this instead of the null check you could avoid the race.
|
# ? Jun 17, 2010 11:26 |
|
tef posted:http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ConcurrentHashMap.html#putIfAbsent(K, V) Unfortunately while the idea of the putIfAbsent is nice, the only way it could ever be effective is if you passed some sort of interface that will create the value, rather than sending the actual value. Imagine if the value came from an expensive database call, you would only want to make that call if it is in fact absent from the cache, but this method would require you to send the value every time. I'm hoping one day they improve that..
|
# ? Jun 17, 2010 13:18 |
|
FateFree posted:Unfortunately while the idea of the putIfAbsent is nice, the only way it could ever be effective is if you passed some sort of interface that will create the value, rather than sending the actual value. Imagine if the value came from an expensive database call, you would only want to make that call if it is in fact absent from the cache, but this method would require you to send the value every time. Eh, those kind of checks aren't in any way or form the job of Map itself but instead the object that utilizes the map.
|
# ? Jun 17, 2010 17:03 |
|
Is it possible to set up data connections using Bluetooth in Java? Everything I've seen on Google suggests that it needs JavaME, and I don't think Android devices support JavaME, just Java. What I'm thinking of is an application that will tunnel Ethernet frames over a Bluetooth connection. This way, I can use my Archos 5 Internet Tablet to connect to a weak Wifi connection in my kitchen (common apartment wifi) and use my laptop in the living room via a Bluetooth link.
|
# ? Jun 18, 2010 04:25 |
|
NecroBob posted:Is it possible to set up data connections using Bluetooth in Java? Everything I've seen on Google suggests that it needs JavaME, and I don't think Android devices support JavaME, just Java. The Android platform is (mostly) a functional superset of J2ME. It isn't really J2ME or J2SE, but odds are if you can do something in J2ME it's possible on Android. A quick google search seems to indicate this is what you're looking for.
|
# ? Jun 18, 2010 11:56 |
|
Internet Janitor posted:The Android platform is (mostly) a functional superset of J2ME. It isn't really J2ME or J2SE, but odds are if you can do something in J2ME it's possible on Android. A quick google search seems to indicate this is what you're looking for. Honestly, I hadn't even looked into the Android side of things yet, but that looks like a good start! Now to see if what I've been reading on the JavaME side is accurate and if the two work together.
|
# ? Jun 18, 2010 17:31 |
|
FateFree posted:Unfortunately while the idea of the putIfAbsent is nice, the only way it could ever be effective is if you passed some sort of interface that will create the value, rather than sending the actual value. Imagine if the value came from an expensive database call, you would only want to make that call if it is in fact absent from the cache, but this method would require you to send the value every time.
|
# ? Jun 21, 2010 10:02 |
What is the difference between something like this:code:
code:
|
|
# ? Jun 22, 2010 22:25 |
|
fletcher posted:What is the difference between something like this: Not really except that the garbage collector cannot reclaim the memory from the last value stored in account until it is out of scope, which is later in the former example. (in general, though you're storing it in a map, so it's not free to be garbage collected anyway at that point.) I would keep the variable inside the scope of the while loop if nothing after the loop needs to know the last account read from resultSet. It's cleaner that way, and avoiding the unnecessary use of local variables is a good thing.
|
# ? Jun 22, 2010 23:15 |
|
When compiling the first version my compiler stores null to the local variable even though the null value can never be read, resulting in a few bytes bigger method. Therefore the second version is more space-efficient. If I remove the null initialization, both versions compile to identical byte-code. If the method is called often enough to be JIT compiled, anything could happen.
|
# ? Jun 23, 2010 09:10 |
|
I'm trying to separate 3 blood pressure readings with dates associated with them. They are stored in a pipe delimited format. Withcode:
code:
code:
|
# ? Jun 23, 2010 12:49 |
|
the javadoc of split() sez:quote:Splits this string around matches of the given regular expression. so you need to escape it! ofcourse "\|" gives an error in java since that's not a valid java escape sequence so use "\\|" as the delimiter code:
|
# ? Jun 23, 2010 13:15 |
|
Aleksei Vasiliev posted:| has a special meaning in regexes (who cares what it is) It's an "OR"? So you're giving split() a delimiter or either "" or "" to split around. There is a "" in between every character, hence it splitting your string into individual characters.
|
# ? Jun 23, 2010 13:47 |
|
I am in posted:When compiling the first version my compiler stores null to the local variable even though the null value can never be read, resulting in a few bytes bigger method. Therefore the second version is more space-efficient. There's also a possible impact on escape analysis and lock coarsening that I honestly wasn't able to measure. Allowing the variable to expire in the while-block's scope allows the compiler to infer that it will never exist outside that region, so it can optimize accordingly.
|
# ? Jun 23, 2010 14:03 |
|
TRex EaterofCars posted:There's also a possible impact on escape analysis and lock coarsening that I honestly wasn't able to measure. Allowing the variable to expire in the while-block's scope allows the compiler to infer that it will never exist outside that region, so it can optimize accordingly.
|
# ? Jun 23, 2010 15:29 |
|
Fly posted:Good catch---I forgot about escape analysis. I'm not sure whether the JIT is smart enough to know that it would work in both cases, but it's possible that it might as the reference is never used outside of the loop. Apparently escape analysis is disabled on OS X: code:
|
# ? Jun 23, 2010 22:12 |
|
Oh hey Eclipse Helios is out. Good timing since my Eclipse suddenly broke overnight. Let's try it out. Hitting "Browse" on the workspace selector on the 32-bit version freezes the program. Pasting the location works, but Eclipse crashes immediately on boot with "org.eclipse.swt.SWTError: Cannot initialize Drop", an apparently unreproducible error that also took out my Galileo. And the 64-bit version can't use Subclipse.
|
# ? Jun 24, 2010 04:01 |
|
TRex EaterofCars posted:Apparently escape analysis is disabled on OS X: Actually "not implemented", Apple just doesn't want to seem lazy. Not that it matters here.
|
# ? Jun 24, 2010 09:11 |
|
Hi, can anyone tell me how to decode this crap someone inserted into a footer.php file on one of my wordpress installs? It is some sort of redirect and there is a big fake .php file filled with this gibberish that I also had to remove and it would be nice to be able to see if it messed with something that is evading my wipe/reinstalls: code:
|
# ? Jun 25, 2010 19:49 |
|
That looks like you may have been cracked. It eventually outputs this:code:
(I did this by using a command-line interpreter to execute the nested unescapes)
|
# ? Jun 25, 2010 20:04 |
|
Okay, thanks!
|
# ? Jun 25, 2010 20:08 |
|
Can anyone explain why I cant seem to get the following code to work with try finally blocks? This is for a gzip compression algorithm: Compress: code:
code:
Here was one such attempt: code:
code:
|
# ? Jun 27, 2010 23:31 |
|
finally-blocks are meant for cleaning up resources. The trouble with streams is that the close-method is also responsible for committing changes made to the stream. In this case the GZIPOutputStream is probably doing some internal buffering, so the underlying ByteArrayOutputStream receives the data too late. To make sure this doesn't happen I usually structure my stream handling like this: code:
|
# ? Jun 28, 2010 00:49 |
|
Thanks for the explanation, I appreciate it. With that in mind, is it possible for me to consolidate my input and output streams since they seem to just be wrapping themselves a few times, and call close on only the parent stream? For instance: This code:
code:
|
# ? Jun 28, 2010 01:47 |
|
Yes. Streams that wrap other streams are supposed to close the underlying stream when they are closed unless there's a reason for them not to, in which case the calling code probably isn't supposed to close the underlying stream in the first place like the output stream for a zip file entry.
|
# ? Jun 28, 2010 03:14 |
|
Why not just call the flush() method on your OutputStreams when you're doing writing to them?
|
# ? Jun 28, 2010 03:18 |
|
I'm having a hell of a time with some jstl expressions. They were working fine last week but I'm getting errors now, I even synced my workspace up with working code and I'm still getting the same error. Below are the details ConfigurationBean.java code:
code:
Any ideas what the problem might be? Everything looks good as far as I can see.
|
# ? Jun 28, 2010 20:50 |
|
I honestly have not touched Java in almost 5 years now (University programming class). So today at work i had this wonderful conversation, with some random senior programmer: I need you to go and do a code cleanup! Uhh... Well this student wrote us this program in java about 4 years ago, it works and we don't want to rewrite it. However now another department needs to expand upon it. But the code is just kind of a trainwreck.... theres no constant code format... it doesn't compile in the last two versions of java, So fix it! When i did java i didn't put much thought into my 100 line programs, and the coding style. I've looked around and seen quite a few different styles, but i can't really find any sort of official coding style from Sun. Does such a thing exist? Also any suggestions to turn this from a monster into something not as ugly.
|
# ? Jul 6, 2010 13:53 |
|
Is this what you mean? http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html
|
# ? Jul 6, 2010 14:05 |
|
|
# ? May 9, 2024 07:05 |
|
Kaltag posted:Is this what you mean? To be fair, those are from the mid to late 90s and haven't been updated ever since. There's stuff in it like "Avoid lines longer than 80 characters, since they're not handled well by many terminals and tools".
|
# ? Jul 6, 2010 15:07 |