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
Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

Right, that makes sense. Thanks, fellas.

Adbot
ADBOT LOVES YOU

Volguus
Mar 3, 2009

MEAT TREAT posted:

As you verified with your code the progression was this: InstrumentedHashSet::addAll -> HashSet::addAll -> InstrumentedHashSet::add() x 3

Even though HashSet calls what it thinks is its own add() method, InstrumentedHashSet's method is the one that is called because that's the type of object that you instantiated.

Anytime you overwrite methods in a sub class, all of those methods will be used in any subsequent calls, even if it's from a super class. This is why Bloch says that you have to allow for inheritance explicitly because you don't know what some sub class is going to end up overwriting and loving your poo poo up.

That is certainly correct, but I can't not think about the way C++ is doing things (explicitly allowing for inheritance) and the pains that model causes.
There are pros and cons to both models, though personally I prefer the Java one where everything is virtual by default. C++'s one cause way too much pain and suffering for non-disciplined developers (99.999% out there).

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
I'm having a problem with a jni call crashing my jvm. I'm passing a byte array from java to some C++ code and most of the time it works just fine, but there is one particular byte array I can pass to the native method that causes the jvm to crash. As far as I can tell it's not specific to the length of the byte array, as there are longer arrays I can pass to the native code just fine.

I started putting cout's everywhere in the C++ code to figure out where it was crashing, and it doesn't even make it to the first cout, which is the very first line of the method. I know next to nothing about C++ and how to properly debug it though, I've just been treating this native method as a magical black box.

I added -Xcheck:jni -verbose:jni to my VM options, doesn't seem to offer me any more clues though. Intellij just spits out "Process finished with exit code 139" when I run my little test program to reproduce the issue. How can I diagnose this thing?

edit: I added -XX:ErrorFile=/home/fletcher/whatever.log to my jvm options, but the error file is never created

fletcher fucked around with this message at 02:51 on Jul 17, 2012

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Is there any specific idiom for "iterate over a list, run a function per object, return the result of the complete iteration"?
In other words, is there a way to generalize all methods like this (which operate inside a list):
Java code:
	public int sumKarma() {
		int total = 0;
		for (int i = 0; i < size(); i++)
			total += get(i).getScore();
		return total;
	}

	public int sumUpvotes() {
		int total = 0;
		for (int i = 0; i < size(); i++)
			total += get(i).getUps();
		return total;
	}

	public Set<String> getAuthors() {
		final Set<String> out = Razdor.newSet();
		for (int i = 0; i < size(); i++)
			out.add(get(i).getAuthor());
		return out;
	}
into a generalized method that takes an object of some kind. I realize this is absurd overkill for the given examples.

fletcher posted:

I added -Xcheck:jni -verbose:jni to my VM options, doesn't seem to offer me any more clues though. Intellij just spits out "Process finished with exit code 139" when I run my little test program to reproduce the issue. How can I diagnose this thing?
Can you post any of the Java/C++ code you're using?
Exit code 139 is a segmentation fault, if it's using the standard numbers, which I would assume it is.

etcetera08
Sep 11, 2008

Aleksei Vasiliev posted:

Is there any specific idiom for "iterate over a list, run a function per object, return the result of the complete iteration"?
In other words, is there a way to generalize all methods like this (which operate inside a list):
Java code:
	public int sumKarma() {
		int total = 0;
		for (int i = 0; i < size(); i++)
			total += get(i).getScore();
		return total;
	}

	public int sumUpvotes() {
		int total = 0;
		for (int i = 0; i < size(); i++)
			total += get(i).getUps();
		return total;
	}

	public Set<String> getAuthors() {
		final Set<String> out = Razdor.newSet();
		for (int i = 0; i < size(); i++)
			out.add(get(i).getAuthor());
		return out;
	}
into a generalized method that takes an object of some kind. I realize this is absurd overkill for the given examples.

You could use this, perhaps. I've never seen anything in the standard library that makes it any easier though.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Guava's transform (and presumably the standard transform thing) is to apply a function per object and return a result per object.

Tesseraction
Apr 5, 2009

It's called a "fold" in Haskell / functional languages.

There's an article discussing implementing it in Java here. Not sure if it's any use for you, though.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
The problem with trying to do functional programming on a micro scale in Java is that there's so much boilerplate involved - the main "wins" of better code clarity and all that just don't materialize. I've heard rumblings of something coming in Java 8 alongside better closures, though.

In the meantime, some synonyms you might try looking for are things like Aggregate, Fold, or Reduce. I know I've seen a couple of Java libraries with Aggregate operations.

etcetera08
Sep 11, 2008

Aleksei Vasiliev posted:

Guava's transform (and presumably the standard transform thing) is to apply a function per object and return a result per object.

Oh whoops you're right I completely misread your post. :(

pigdog
Apr 23, 2004

by Smythe

Aleksei Vasiliev posted:

Is there any specific idiom for "iterate over a list, run a function per object, return the result of the complete iteration"?
...

There are other alternatives, but we use LambdaJ a lot.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Aleksei Vasiliev posted:

Can you post any of the Java/C++ code you're using?
Exit code 139 is a segmentation fault, if it's using the standard numbers, which I would assume it is.

Unfortunately I don't think I can. I tried updating from jdk1.6 u26 to u33, still crashes with no hs_err_pid.log file to be found. I'll see if I can make a reduced test case.

fletcher fucked around with this message at 00:48 on Jul 18, 2012

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
The maximum length of the array I can pass before it crashes appears to be controlled by the -Xss jvm parameter (stack size). Does that sound right? How big can I set this value to? Is there a better solution than simply increasing the limit?

fletcher fucked around with this message at 01:29 on Jul 18, 2012

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Finally got a core dump for that JNI issue I was having, some sort of bug in one of the libraries that the native code uses.

Had to do the following on Ubuntu 10.04 to get the core dump: http://chrisjakeway.wordpress.com/2011/12/09/ubuntu-10-04-enable-core-dumps/


On to a new issue! How can I store a java object in memcached that does not implement Serializable?

Doctor w-rw-rw-
Jun 24, 2008

fletcher posted:

Finally got a core dump for that JNI issue I was having, some sort of bug in one of the libraries that the native code uses.

Had to do the following on Ubuntu 10.04 to get the core dump: http://chrisjakeway.wordpress.com/2011/12/09/ubuntu-10-04-enable-core-dumps/


On to a new issue! How can I store a java object in memcached that does not implement Serializable?

Marshal it yourself. Maybe try Jackson, and store that, or use the binary extensions if you want it to be more compact.

And dear god, if nobody's holding a gun to your head, stay away from Serializable on servers. You want uptime and upgradability, not corrupted objects because of version mismatches between objects.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
You could always try Google Protocol Buffers. It's a wire format but it should work fine.

Max Facetime
Apr 18, 2009

I was pleasantly surprised by GSON's ability to fill fully generic containers inside objects correctly without needing to add extra annotations or getters.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Thanks for the suggestions.

We use protobuf for some other stuff. There are tons of different objects from a 3rd party library that I want to cache so I was hoping to avoid writing/maintaining tons of message definitions, need to investigate this though. It's mostly metadata information from a SOAP API, maybe there is something I can do to cache the underlying XML? I wonder if it's expensive to be parsing all the XML over and over though.

Doctor w-rw-rw-
Jun 24, 2008

fletcher posted:

Thanks for the suggestions.

We use protobuf for some other stuff. There are tons of different objects from a 3rd party library that I want to cache so I was hoping to avoid writing/maintaining tons of message definitions, need to investigate this though. It's mostly metadata information from a SOAP API, maybe there is something I can do to cache the underlying XML? I wonder if it's expensive to be parsing all the XML over and over though.

It's CPU bound so if your workload leaves your I/O saturated or memory full, then you should be fine.

geeves
Sep 16, 2004

I am in posted:

I was pleasantly surprised by GSON's ability to fill fully generic containers inside objects correctly without needing to add extra annotations or getters.

Yeah GSON is great. Just make use of their exclusions, especially if you use Hibernate and have associated mappings. It will cry foul about circular dependencies (even if they're not)

Lars
Jul 6, 2004

Better Lucky Than Good.
(since 1994)
Sorry for the lowlevel question, but I'm (hopefully) starting a diploma in IT next spring, and their Introduction to programming class is Java based. Do you guys know any good online resources to give me a good head start? I've worked my way through some of MITs Open-Courseware lectures (it's based on Python, but seem valid to learn some fundementals), but it seem rather silly to go all nuts in a language that I'll "abandon" in 5 months. So, any pointers?

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

The official Java documentation's tutorials are surprisingly good. If you can get through the "Essential Java Classes" module, you'll be laughin' for any first-year Java class.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Anybody here have much experience with Gson? I'm trying to use less of a hack method to accomplish something.
I have an object with a bunch of fields, one of which is a long. However, the JSON has it as a non-integer, e.g. 1342911054.0.
Gson can handle that just fine as I'm not using a custom deserializer for the object. If I am, it fails. Unfortunately I need to use a custom deserializer for what I'm doing.
Reduced example:
Java code:
package su.slytherin.experiments;

import java.lang.reflect.Type;

import com.google.gson.*;

public final class Experiments {

    public static void main(String... args) {
        String json = "{\"value\": 1342911054.0}";
        System.out.println(GSON_CUSTOM.fromJson(json, Example.class));
      //Expected: Example [value=1342911054, special=void]
    }

    private static final Gson GSON_STD = new Gson();
    private static final Gson GSON_CUSTOM;
    static {
        GsonBuilder gb = new GsonBuilder();
        gb.registerTypeAdapter(Example.class, new Example.Deserializer());
        GSON_CUSTOM = gb.create();
    }

    private static class Example {
        private long value;
        private transient Object special; // needs custom deserialization

        @Override
        public String toString() {
            return "Example [value=" + value + ", special=" + special + "]";
        }

        static class Deserializer implements JsonDeserializer<Example> {
            @Override
            public Example deserialize(JsonElement json, Type typeOfT,
                    JsonDeserializationContext context) throws JsonParseException {
                Example ex = GSON_STD.fromJson(json, Example.class);
                ex.special = Void.TYPE;
                return ex;
            }
        }
    }

}
On running it throws a number format exception:
Caused by: java.lang.NumberFormatException: For input string: "2911054.0" (Note the number is wrong)

I don't know if this is due to an issue/inconsistency with Gson that I should report, or if I'm loving up somewhere.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
What's happening is that the TypeAdapter that GSON ends up using for your example is ReflectiveTypeAdapter, which is seeing the long value; field in the Example class, then tries to use a parser for Long which then craps out on that number.

I'm not sure how you register new TypeAdapters but there is a List <TypeAdapterFactory> factories object inside GSON (Gson.class:120) that it looks for TypeAdapterFactory classes that support your class. It iterates through that List though (Gson.class:352) and once it finds a potential match it plows on ahead, which indicates to me that it'll be hard for you to fix this without using maybe BigDecimal instead of long.

I did this based on the current SVN head so if you're using a different version obviously ymmv.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
Workaround was not actually too difficult once I figured it out, just weird: http://pastebin.com/qsXCvs8w (see lines 33 and 62 on)
Just needed to reproduce the default behavior (that workaround class is pretty much just copy-paste). No idea why. Maybe an ordering issue?
It seems like I shouldn't need to do this, so I've reported it to Google. Plus I have no idea if that workaround has any side effects.

edit: It's because it's using a JsonTreeReader to parse the JSON, instead of a JsonReader. It extends JsonReader and overrides its methods but its implementation doesn't have the fallback double parsing that JsonReader does.

Malloc Voidstar fucked around with this message at 01:46 on Jul 22, 2012

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
What's the right way to tune the MaxPermSize jvm parameter? Just keep increasing it until you no longer see a java.lang.OutOfMemoryError: PermGen space?

Max Facetime
Apr 18, 2009

fletcher posted:

What's the right way to tune the MaxPermSize jvm parameter? Just keep increasing it until you no longer see a java.lang.OutOfMemoryError: PermGen space?

Pretty much.

Unless you are running something like Tomcat and doing a lot of redeploying of webapps without restarting Tomcat, then your libraries likely have ClassLoader leaks and no amount of PermGen will be enough.

Here's some interesting information on how Tomcat tries to help with these leaks:
http://wiki.apache.org/tomcat/MemoryLeakProtection

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.
Another way to do it is to literally count up all your class file sizes, including jars. It's a lot of stupid work and you need to be thorough, and that also doesn't include rt.jar or any runtime generated classes (say from Spring proxies or Hibernate proxies).

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Just wanted to add that WebServices also love to chew through the permgen space when you redeploy them a lot.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

MEAT TREAT posted:

Just wanted to add that WebServices also love to chew through the permgen space when you redeploy them a lot.

If this happens to you then you must have a ClassLoader leak, or a configuration where the jvm is not allowed to reclaim objects in permgen.

Scaramouche
Mar 26, 2001

SPACE FACE! SPACE FACE!

Have any of you guys set up the red5 server/recorder? I'm having a hell of a time at the last step where you have to compile a client app in Eclipse. The problem is I have no understanding of Java Development or deploying applications or Java programming or Eclipse at all. I'm mostly following these instructions:
http://stackoverflow.com/a/5746747
The instructions there work for me in that Eclipse doesn't give me any error messages on compile, so I think my application is okay, but it doesn't actually do anything. The only instructions I can find otherwise look crazy complicated, and assume I'm running Apache and compiling everything from scratch: http://red5guide.com/


The server is set up and working, just can't make the red5recorder application work at all; it errors out saying 'can't connect to rtmp://127.0.0.1/(appname)'. I've been on this for at least a day and I'm sure it's because I don't know how to work with Java projects at all.

I'm posting this in the faint hope that someone else has set up this project specifically and knows off the top of their head what might be going wrong. Otherwise, I'm going to have to delete everything, and start over with the instructions from red5guide.com even though I don't want to.

EDIT-I realize I gave nothing for anyone to go on to actually help, and for that I apologize. This has been frustrating, not because I magically expect Java to work like I want, but rather because I know the problem is my own ignorance. But I have advanced things somewhat and I am now getting actual error messages. The first is:
code:
The import org.red5.server.api.IScope cannot be resolved
Which leads to here, a posting from one of the Red5 maintainers:
https://groups.google.com/forum/?fromgroups#!topic/red5interest/QVf2h81wvVE

He says to:
The import org.red5.server.api.IScope cannot be resolved
- Update red5 jar or source with latest
- Correct location of IScope is org.red5.server.api.scope.IScope, previously it was org.red5.server.api.IScope

See, I get this; outdated reference. The thing is, I don't know where to update it. I have the latest Red5.jar (Version 1.0RC2 Created 5/21/2012) that I can find. I've searched for the old reference in-code and it doesn't show up anywhere. I installed the server from a prebuilt binary of 1.0.0-RC2.


Scaramouche fucked around with this message at 00:52 on Jul 27, 2012

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

trex eaterofcadrs posted:

a configuration where the jvm is not allowed to reclaim objects in permgen.

I'm pretty sure it's this, or else GlassFish's class loader has the leak, can't be arsed to find out.

notMordecai
Mar 4, 2007

Gay Boy Suicide Pact?
Sucking Dick For Satan??

an odd problem occurred when I installed some git software within Eclipse and now my entire toolbar looks shrunken and hosed up. it's causing muscle memory movements of my mouse to slow work.



After Eclipse restarted, my toolbar looks like this now and not like when I installed it several months ago, stuff is out of order and some things are entirely missing. Is there a way to fix this, I can't seem to find the option to.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

MEAT TREAT posted:

I'm pretty sure it's this, or else GlassFish's class loader has the leak, can't be arsed to find out.

If you're using Spring or something else that uses CGLIB (or other proxying mechanisms?), then Glassfish/Tomcat/etc. all seem to have this problem. I think Oracle blamed it on Spring, and Spring blamed it on CGLIB, but nobody ever bothered to fix it.

Sedro
Dec 31, 2008
Is there a way to create a file only if it doesn't exist, free of race conditions? I want this behavior:
File exists -> throw exception
File does not exist -> atomically create the file and increment usage count

Max Facetime
Apr 18, 2009

Sedro posted:

Is there a way to create a file only if it doesn't exist, free of race conditions? I want this behavior:
File exists -> throw exception
File does not exist -> atomically create the file and increment usage count

I don't know what usage count means in this context, but java.nio.file.Files.createFile(Path path, FileAttribute<?>... attrs) sounds like it would do that.

Edit: out = Files.newOutputStream(path, StandardOpenOption.CREATE_NEW); is probably implemented like those, but that's from 1.7 too.

Max Facetime fucked around with this message at 02:46 on Jul 28, 2012

Sedro
Dec 31, 2008

I am in posted:

I don't know what usage count means in this context, but java.nio.file.Files.createFile(Path path, FileAttribute<?>... attrs) sounds like it would do that.
By usage count I mean opening and obtaining a handle/native resource to the file from the operating system. Locking the file would be fine too.

That API appears to do what I want, but unfortunately I'm on Java 5 and unable to upgrade. (edit: actually it doesn't do what I want. For some reason I thought it returned a stream.)

Edit: I want the standard behavior of the FileOutputStream and RandomAccessFile constructors, except I want an exception if the file already exists. I could use O_EXCL in Unix, FileMode.CreateNew in .NET, etc. I can't find Java support for this anywhere.

Sedro fucked around with this message at 23:20 on Jul 27, 2012

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
What do you actually want to do?

If you're working with other processes that are co-operating on a locking scheme, use the FileLock class (which maps to whatever file locking operations the OS natively provides, if you're co-operating with other non-Java processes).

Safe and Secure!
Jun 14, 2008

OFFICIAL SA THREAD RUINER
SPRING 2013
Let's say that I claimed in an interview that I was a Java wizard, and this set my interviewer off on a mission to ask me increasingly difficult questions about Java to if I was lying. What kinds of questions/topics would I be asked about?

Out of all the languages I've used, I've got by far the most experience and am most comfortable in Java, so I figured that it would be best to focus on improving the area I'm good at if I want to get the best job I can when I graduate this Spring. What knowledge separates good, highly-experienced Java-using software engineers from the rest?

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
There are a lot of deep crevices in the Java language that are not covered in most school curriculums. If I was trying to "break" a candidate I might ask about the meaning of the final keyword in various contexts, synchronized for blocks versus methods, or some of the finer details of how nested and anonymous classes work. If that doesn't work, I'd ask some questions about the generics system. In addition to practical matters of semantics there's quite a bit of trivia that could be pulled out- labeled breaks, unicode expansion rules and unsigned types, etc. If you haven't already, I recommend reading Java Puzzlers- at minimum it will help you find holes in your understanding.

I'd also expect someone claiming to be skilled with Java to be able to write simple programs without an IDE. If you can't remember how to use Lists and Maps without syntax completion or consulting a Javadoc, I find it hard to believe you've written anything complex.

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe
Great post Internet Janitor, I just also wanted to add that understanding how the Java serialization framework works is also one of those language features with lots of gotchas there's also the little known Weak References and finally I'd also say that a Java wizard should know how the different garbage collectors work and know how to tune some basic JVM parameters.

Janitor Prime fucked around with this message at 18:38 on Jul 28, 2012

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