Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Paolomania
Apr 26, 2006

Chairman Steve posted:

Baby steps. Let's get Java 7 out the door first.

somebody is hot for the invokedynamic opcode!

Adbot
ADBOT LOVES YOU

Paolomania
Apr 26, 2006

IMlemon posted:

I want to execute my algorithm in steps so that I can display the results nicely. User presses MoveButton, something is done and something is diplayed. He does that again, again something happens. Repeat. I don't want to completely block the application, user should still be able to select items and click buttons and whatnot.

When the UI event happens, you'll want to use SwingUtilities.invokeLater to schedule a task on the AWT event queue.

Paolomania
Apr 26, 2006

Anyone know what might cause a class loaded by the tomcat system classloader to later not be found? This is an instrumentation jar used for -javaagent that also gets added via tomcat's setenv.sh. Running -verbose confirms it gets loaded:
code:
$ grep StringInstrumentation ../logs/catalina.out
[Loaded test.StringInstrumentation from file:/some/path/to/tomcat/bin/test.jar]
Exception in thread "main" java.lang.NoClassDefFoundError: test/StringInstrumentation

Paolomania
Apr 26, 2006

Paolomania posted:

Anyone know what might cause a class loaded by the tomcat system classloader to later not be found? This is an instrumentation jar used for -javaagent that also gets added via tomcat's setenv.sh. Running -verbose confirms it gets loaded:
code:
$ grep StringInstrumentation ../logs/catalina.out
[Loaded test.StringInstrumentation from file:/some/path/to/tomcat/bin/test.jar]
Exception in thread "main" java.lang.NoClassDefFoundError: test/StringInstrumentation

Looks like my class was not visible to classes loaded by the boot classloader. I fixed this by running with -Xbootclassloader/a.

Paolomania
Apr 26, 2006

KuruMonkey posted:

Edit:
I am still slightly confused as to why this:
code:
String input = "this is a string";
char[] characters = input.toCharArray();
List charlist = Arrays.asList(characters);
Gives me a List with one entry; the array of characters. Because "characters" is not an array of Object or something descended from it, right?

nevermind. check the contract at [url]http://download.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html#asList%28java.lang.Object[]%29[/url]

it expects an Object[]

Paolomania fucked around with this message at 20:27 on Oct 21, 2010

Paolomania
Apr 26, 2006

zootm posted:

Obviously because Java primitive arrays are not generic in any way this would need to be implemented by hand (or using a script) for every primitive you might want to shuffle arrays for. Which is a complete pain in the arse.

If you take a look at java.util.Arrays, this is exactly the kind of functionality that this class's static methods handle. The lack of shuffling functionality for primitives could be considered a hole in this class's functionality.

Paolomania
Apr 26, 2006

Dransparency posted:

It's even weirder because Collections.shuffle() purports to do exactly this.

It is not really that weird. Java arrays and collections are very different containers for data. Collections are Objects and are high level, with their methods called virtually and their elements working fundamentally on the level of Object references which allow you to apply generics. Arrays in Java are not really a primitive and not really an Object - they get passed around by reference like a Objects, but arrays have their own opcodes for allocation and access built into the JVM so array operations are not virtually dispatched method calls, they are low-level C-like operations and not at all generic in that they are specific to the primitive element type.

A statement like "new char[10]" gets compiled into a "newarray" opcode, which once inside the JVM effectively turns into a native call to something like "malloc(10 * sizeof(java_char))". The JVM also has separate opcodes for moving values into/out-of arrays (one load/store opcode pair for each primitive type, in fact) so again these are effectively very C-like and there is no virtual method dispatching so the run-time performance should be very good. For Collections, all of these operations are going to involve virtual method calls at run time (literally the invokevirtual opcode).

Paolomania
Apr 26, 2006

It seems to me that you would like the brevity of some of the scripting languages that you are used to, along with decent performance. I think the solution in your case is to take advantage of the mutability and speed of arrays and write your own C-style static shuffle utility like zootm suggested. This will allow you to write:

code:
public static String shuffleString(String in) {
   char[] characters = in.toCharArray();
   shuffle(characters);
   return new String(characters);
}
Where basically this is a convenience function that copies your immutable String to a char[], performs the shuffle on the mutable char[] using your utility, and returns a new immutable String copied from that result.

Paolomania
Apr 26, 2006

Elos posted:

Am I doing something really stupid here or is the Sun linux JVM to blame?

This white paper says that as of JRE 1.4 they made graphics improvements across all platforms, including Linux. So maybe the Shared Memory Extension for X is turned off (thus causing tons of buffer copies between your program and the local X server), or maybe various threading events are waking up your game loop thread early causing it to run faster than 20Hz (why are you constantly redrawing a roguelike anyways?), or maybe something weird is going on in your levelHandler.drawTest(g) (which we can't see in that code sample).

Paolomania
Apr 26, 2006

Jewbert Jewstein posted:

Maybe rephrasing my question will help some goon find the answer. Say I have a String that contains an HTML file. Is there a way to upload that file so that it appears as an embedded window in my application?

I think the problem lies in the fact that you need more than just an HTML renderer - you also need a JavaScript evaluation engine. I am not familiar with Google's library, but standard Java components such as JEditorPane that can render HTML have no ability to interpret JavaScript.

Paolomania
Apr 26, 2006

fletcher posted:

If I do something with an array, and then I'm done with it and I need to do something else with an array, is it better to reuse the same pointer?
These both get compiled into stack local variables, so its just an extra 32-bit address on the stack. If you are writing some massive recursion where you are worried the size of each stack frame the cost of using an additional variable is pretty much free when compared with the allocation and use of the arrays themselves.

quote:

How does it effect the GC?
Java GC does not work like STL auto pointers. Although there are slight differences in when the first array becomes eligible for GC, practically speaking these are not going to get GC'ed until there are memory pressures or the GC gets bored.

quote:

How about readability?
Two arrays is more readable, but I disagree with Mustach. If you are doing a bunch of computations that belong together then keep them together.

Paolomania
Apr 26, 2006

Elos posted:

Graphics2D.drawImage seems to be the culprit here since it's what Title.draw calls.

So it looks like draw performance. A few things to try:

- look into whether or not the shared memory extension is turned on. If not, your application is doing extra buffer copies to the X server.

- use GraphicsConfiguration.getImageBufferCapabilities() to see if you have access to all the hardware acceleration that you think you do. Try turning off transparency to see if that one rendering option is the culprit.

- perhaps that image processing you are doing to create the alpha values is causing the JVM to treat your images as unaccelerated buffers that need to be kept both in the heap as well as in video RAM. You could maybe try some tricks to try to force the JVM to store your images in video RAM (see here).

Paolomania
Apr 26, 2006

Elos posted:

getImageCapabilities().isAccelerated() returns false which I presume is a bad sing.

Big question: which linux Java compiler and JVM are you using? I have had issues with gcj, which is the default Java on Debian/Ubuntu, missing functionality.

Paolomania
Apr 26, 2006

OpenJDK also looks like it had alot of closed-source native code removed from the graphics libraries. You might have to try the Oracle JRE to see if that makes a difference.

Paolomania
Apr 26, 2006

Since the additional process fork leaves you with no connection to the process that actually does the work there is no way to get a notification of its exit from within Java. You would have to just wait a reasonable amount of time or poll the file status (which also involves waiting). Since you know that this is Windows-specific I would wrap the call to dxdiag in a shell script and run (and wait on) the script from Java instead.

Paolomania
Apr 26, 2006

Aleksei Vasiliev posted:

Okay, what's different between cmd and a batch file? Running dxdiag from cmd returns immediately, just like Java, but a batch file waits for it to finish. why are batch files magic

http://blogs.msdn.com/b/powershell/archive/2007/01/16/managing-processes-in-powershell.aspx

My guess is it has something to do with automatic piping.

Paolomania
Apr 26, 2006

Yeah, if what you are trying to do is so windows-graphics-specific, why are you trying to do it in a Java app?

Paolomania
Apr 26, 2006

I'm pretty sure that NetBeans is king of the hill with Java GUI building, but if you want to use Eclipse there is the Visual Editor Project which can help you build GUI apps from a variety of frameworks (including Swing). Its a bit clunky but it has alot of functionality.

Paolomania
Apr 26, 2006

As with other languages, a case will fall-through if there is not an explicit break. So what you want to do looks like:

code:
case 1:
case 2:
case 3:

   // do stuff
   break;

Paolomania
Apr 26, 2006

If you guys want to pedantic, LISP cond and case statements have no fall-through, but in those situations you can use more complicated predicates for your conditions and lists of values for your cases, respectively.

Paolomania
Apr 26, 2006

I may be wrong, but I think that content panes, not being input widgets, never get input focus.

edit: I think that input map is for things like text boxes so you can do things like automatically updating a search as you type, etc.

Paolomania
Apr 26, 2006

D0nk3y_ posted:

Try cleaning the project and rebuild it. Eclipse's autobuild can do funny things.

This is especially true when using Eclipse in conjunction with external tools (such as ant run outside eclipse, or an external version control system) as some changes to the file system never send callbacks to eclipse letting it know that files have changed on disk. In these cases, you can manually request that eclipse sync with the file system by right-clicking on the project root and selecting "refresh" from the context menu.

I would run into this problem all the time when working on a large hetero-genus project that had one eclipse sub-project. We did version control for the whole thing using cygwin svn, which had subtle incompatibilities with eclipse when it came to ssh tools, so all version control operations were handled outside of eclipse. This meant we had to manually refresh the eclipse project within the editor every time we updated externally.

Paolomania
Apr 26, 2006

Mirconium posted:

How do I make a thread listen for input for a specific amount of time, then start doing something else? I've looked into timers, and they're really retardedly complicated for what seems like it should be a simple task. Thread.sleep also doesn't work.

It sounds like you want a worker thread sleeping while an input thread buffers input for the "set amount of time", at which point the worker thread wakes up and handles all the input that was buffered over that interval.

Paolomania
Apr 26, 2006

Micro, what is the context in which you are trying to do this? I mean there are hackish things you could do such as:

code:
try { Thread.sleep(SET_AMOUNT_OF_TIME) } catch (InterruptedException e) { /* don't care about spurious wakeups */ }

int available = System.in.available();
if(available>0) { 
   System.in.read(someBuffer,0,available);
}

Paolomania
Apr 26, 2006

If you only have to worry about a small number of keys and it is a GUI app then you can use Swing key maps and compare the timestamps of sequential key presses.

Paolomania
Apr 26, 2006

Aleksei Vasiliev posted:

Anyway, why the hell does this code do this?

My guess is that it is a race condition. Both scanners think they can grab the same item out of some System.in input buffer, one of them consumes it and the next one barfs and dies when it is not there.

Paolomania
Apr 26, 2006

Ha what a lazy reader I am. Didn't even see the closes.

Paolomania
Apr 26, 2006

epswing posted:

Just how big IS the cost of exceptions? Writing a legitimate application without exceptions sounds awful. Would all methods return int and you'd have some giant lookup table so you'd know what to do with error code 6233? And working with the libraries forces you to try/catch/throw anyways, so you've got to deal with those exceptions regardless.

See Josh Bloch's "Effective Java" 2nd ed, Chapter 9.

edit: just read the whole book, it is awesome.

Paolomania
Apr 26, 2006

What language are you coming from? It is fairly clear that you are not "thinking in Java".

Paolomania
Apr 26, 2006

especially if you are fond of using HashMap and HashSet

Paolomania
Apr 26, 2006

TRex EaterofCars posted:

Inner classes (and closures in other languages) use $ to denote their "inner-classness" when javac builds their class files. It's probably just so you never run into a situation where an inner class name collides with a variable you made.

They wouldn't collide. Inner classes look like FooClass$BadName (inner class BadName) as opposed to FooClass.$BadName (class member $BadName).

Paolomania
Apr 26, 2006

MariusMcG posted:

I have a JPA question...

Let's say I have a Person entity, and I want to retrieve a collection of Person objects via a JPQL query. But I only need the firstName and lastName fields, whereas the Person entity has fifty fields on it. For optimal performance, how do I get a collection of Person entities that contain only the fields I need? Is there a way to do it that doesn't require me to mark every other field as lazy fetch?

Thanks very much!

So you basically want to do a SELECT of only a few specific rows out of a much larger table. This would be like getting only "partial objects" and kind of breaks some ideas of Object Orientation and Object-Relational-Mapping, so I doubt there is a good way to do it through JPA. What you probably want to do is fall back on raw SQL.

Paolomania
Apr 26, 2006

Internet Janitor posted:

StringBuffers differ from StringBuilders in that they are thread-safe- this also makes them slightly slower.

This. If you diff the source the only real difference is a "synchronized" on the front of every funciton. This means that at runtime the JVM will do some extra operations to wait for, acquire, and release the monitor for a StringBuffer before and after every method call.

Paolomania
Apr 26, 2006

fart factory posted:

I'm having an issue with a broken pipe exception within a java program. The exception occurs when the server recieves an unusually long xml message. I've read that this can cause the broken pipe because the writing thread has already finished by the time the reading thread has processed the message and sent the response. Some sort of countdown latch has been proposed, but the thread that is writing the data to the server is part of a different project. Is there any other way to ensure that the thread will stay running until all the data has been read/response has been sent?

It is not that difficult to make a thread wait around for some condition, but without more specifics about your programs and how much control you have over each side of the pipe (do you control both the client and server code? are you using HTTP or some ad-hoc XML protocol?) it is hard to make a recommendation.

Paolomania
Apr 26, 2006

Are you intending to have dynamic word dictionaries? If so, you will need to maintain alot of extra structures yourself behind the scenes. Also, I imaging having more than one value on the return stack will be problematic. Also, I am almost certain that Forth has alot of symbols that are valid word names that Java would barf on (such as '*') even if you use something like dynamically adding methods glommed onto one main dicitonary object and the new invokedynamic opcode to get at them. IMO you will have much more success implementing a Forth interpreter.

Paolomania fucked around with this message at 06:56 on Jan 8, 2011

Paolomania
Apr 26, 2006

Another basic problem is that Java stack frames are fairly isolated from eachother. From http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html section 3.5.2 :

quote:

Because the Java virtual machine stack is never manipulated directly except to push and pop frames, frames may be heap allocated. The memory for a Java virtual machine stack does not need to be contiguous.

So you never really have the option of leaving a "return stack" on the stack because as soon as you return, your local stack frame is gone and only the declared return type is left on the top of the calling stack frame. Perhaps you could package the return values as some Object array of known size which you then explode onto the calling stack after you return.

Paolomania
Apr 26, 2006

Luminous posted:

Man, I saw the question, and starting reading the replies, just waiting, hoping, nobody would post this so that I could do so. This is a really great book for understanding threading issues in general, and getting a grip on what Java can provide to you.

This book keeps good company.

Paolomania
Apr 26, 2006

Two Percent posted:

Thanks for the answers, and sorry it took me this long to write back.

I'm familiar with decompiling the bytecode, the problem being, I don't have the bytecode. When I run the program, it accesses unknown JARs from wherever in my computer, and I would like to know if it's possible to extract those JARs from the RAM, or at least their adresses.

You can run the JVM in verbose mode and it will list JARs as they are loaded.

Paolomania
Apr 26, 2006

And if you are stuck on Windows, sysinternals Process Monitor can give you similar information.

Adbot
ADBOT LOVES YOU

Paolomania
Apr 26, 2006

Malfeasible posted:

Whether inside double quotes or in the (String[] args) parameter in main, in my mind in both scenarios Java is creating a String from data containing the '\n' character sequence, but is parsing it as a newline in one case and as a literal char sequence in the other case. This still weirds me out!

Your mental error is in thinking of Java as an interpreted language instead of as a compiled one. It is the Java compiler that changes the newline escape sequence '\n' into a unicode newline character. In the compiled class file the sequence of characters "\n" never appears in string literals (unless the programmer originally wrote "\\n"). None of the Java methods do any conversion of escape sequences at run time. "\n" is just a language level construct that instructs the compiler to "put a newline character here when you compile this", and not something like HTML's "%0A;" which gets interpreted at run-time.

Paolomania fucked around with this message at 19:42 on Feb 5, 2011

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply