Tag Archives: java

A cute hack: Changing the scope of System.out

This isn’t any sort of deep trick. I just found the results surprisingly pleasant, so thought I’d share.

In the GUI interpreter I need to be able to capture standard out. Doing println in the interpreter should print to the screen, not the console. And that’s fine. Java provides System.setOut. I had to fight the stupidities of java.io a bit to make it work (annoying details with flushing among other things), but it did in the end.

There is of course a problem with this. System.out is basically a great honking big global variable. We’ve now made it impossible to run multiple interpreters in the same VM. Le sigh.

Ok. So, what we do is a bit of juggling and set System.out appropriately before interpreting each thing. Right?

Nope. Doesn’t work. These things can and do live in separate threads. And for that matter, what about background threads spawned by one of the interpreters?

The solution is to make System.out thread local. And not just any type of thread local – an inheritable thread local. Spawned threads need to inherit the System.out of their parent thread so that something like spawn { println(“Hello world”) } prints to the right screen.

Once you have this, the idea is obvious. You define an object which extends OutputStream but delegates all functionality to some thread local variable (snarfing the System.out that was present when the object loads as the default value), set System.out to be a print stream wrapping that and you’re done. You can now set its value in a thread local way and multiple interpreters can coexist peacefully.

Like I said, nothing special, but it seems like a surprisingly handy trick.

This entry was posted in programming and tagged , , , on by .

Request for help: Numerical linear algebra

I have a bit of an odd background. I have a fair bit of mathematics (four year MA equivalent course in mathematics at Cambridge) and at least a reasonable background in software engineering and programming (about three years working as a software engineer now, plus a lot of random stuff on my spare time during that period and a little bit before hand), but I don’t have a lot of knowldge of the overlap between the two. This is increasingly causing me trouble, and I’d like to fix it. It seems like the sort of subject my blog audience should know about, so I’m asking for help here. :-)

The immediate subject I want to learn more about is numerical linear algebra. It’s come up a couple times recently and I’ve had to go “Bwrgh. Um. Halp!”. I usually muddle through, but it’s really not something I should be struggling on and I’ve probably done stupid things as a result of lack of knowledge.

So, my goal here is to become reasonably well versed in at least the basics of the subject. At the moment nothing terribly specific – I want to get a good grasp of the theory and practice of working with matrices, vectors, etc. computationally. Linear optimisation and eigenvector problems especially. I’m looking for recommendations for books, libraries, software packages and anything else you think would be useful. I’m willing to spend a reasonable amount of money (more so on the books than the software packages, as long term I’ll really be looking for stuff I can freely bundle with my personal projects) in doing so, ‘though obviously free is a plus. Libraries I can use from a variety of languages are definitely a good thing, though I’m willing to look at language specific things like NumPy, etc. too if they come highly recommended (at least as a starting point).

Relevant background:

Mathematics: Quite a lot of real and complex analysis, though mostly pure. Enough linear algebra that I at least know where to start with it, though I’m going to be rusty.

Programming/computer science: Some amount of general algorithms and datastructures work. Generally comfortable with Java, Haskell and Scala. Comfortable enough with C that I can write it if I have to. A little bit of Python and Lua (plus a few others which are probably not relevant). Fairly willing to pick up a new language if it would help (though if you tell me to write fortran I’ll cry).

So, suggestions?

Edit: I’ll keep a list of recommendations updated here. If you have any comments for or against these, please pipe up.

I found a copy of Epperson going very cheaply and for no particular reason it looked interesting, so I’ve ordered a copy. I’ll almost certainly order a copy of Golub and Loan. I’m also going to be playing around with Octave. Thanks guys!

This entry was posted in programming and tagged on by .

Not really an announcement: GUI frontend to Scala Interpreter

So, I’ve finally got sick of the unusable command line interface to the Scala interpreter.

Don’t get me wrong, I like command line interfaces. But I’ve always found command line interfaces written in Java to be pretty horribly unusable, and the variety of different quirks shown by the scala one between windows and linux is pretty gruesome.

Anyway, the upshot is that I’m building a simple GUI frontend to it. It turns out that the interpreter API is actually really easy to work with – it only took me about half an hour to hack together a basic working frontend (after some… minor issues with Java’s IO API)

So far this isn’t really in a state that other people can use it – no proper buildscript, the GUI frontend is hacked together in the interface designer and doesn’t handle resizing properly at all (so is probably broken on any resolution but mine). This is more of a “If you want to look at the code and see how to do it” than anything else. It’s available in my misc hg repository.

A particular reason you might want to look at the code is that it uses QT Jambi rather than Swing, so if you want a Swing version you’ll have to write it yourself and might find this a useful starting point. Why? Well, two reasons. Firstly, I like the results and API of QT better than Swing’s. Secondly, Swing doesn’t work on my home PC so it would be pretty pointless for me to use it.

This entry was posted in programming and tagged , , , , , on by .

Make your menu definitions less irritating

Anyone who has written a non-trivial amount of Swing will know the pain of menu definitions. They’re amazingly verbose.

Metascope used a reasonably large number of menus, and this verbosity had started to bother me, so I came up with a few syntactic hacks to clean it up. A “DSL” if you prefer. The result was relatively terse in comparison (only one superfluous line of code and two superfluous lines for the braces per menu item! In Java terms that’s practically a one liner).

Here’s essentially the same idea ported to Scala, with a few additional cute hacks and taking advantage of Scala’s first class functions to make it terser yet.

Some sample code:

val frame = new JFrame(){}

frame.setJMenuBar(new MenuBar{
new Menu("File"){
"Save" does { }
"Open" does { }
---
"Quit" does { System.exit(0) }
}
new Menu("Edit"){
"I like kitties" is true toggles (x => if(x) println("I like kitties") else println("I appear to be lying"))
new Menu("A sub menu"){
"There are no kitties in this submenu" does { }
}
}
});

frame.setSize(500, 500);
frame.setVisible(true);

The rest of the code to make this work is available here.

This entry was posted in programming and tagged , on by .

“Object Main extends Application” considered harmful

You’re probably all familiar with the standard Java invocation for creating an applicaiton’s start point.

class Main{
public static void main(String[] args){
// code
}
}

Scala has a nice feature which lets you use a trait to define your application where you don’t care about the initial arguments.


object Main extends Application{

//code

}

What happens is that Application has a method main(args : Array[String]) which gets inherited, then a corresponding static method is created on the class Main. The application logic goes in Main’s initialisation code. Neat, huh?

All we’re missing is what the definition of main is.


trait Application{

def main(args : Array[String]) = createHorrificallyConfusingBugsIfYouSoMuchAsLookAtAThread();

}

Oops.

Ok, that’s not actually true. Really it’s


trait Application{

def main(args : Array[String]) = ()

}

It doesn’t do anything. Everything happens in the object initialisation code.

Why is this an issue?


object Main extends Application{

val foo = stuff();

spawn {  doSomethingWith(foo) }

doSomeLongRunningThingThatBlocks();

}

Why is this an issue?

Well, foo is a field. And Main’s constructor hasn’t completed. This means that the visibility of foo to other threads is completely undefined. Bad things will happen.

This isn’t a hypothetical scenario. We’ve just spent a non-trivial amount of time debugging a problem caused by this. We were spawning a background process from Main, and then entering QApplication.start() (the “let QT start doing stuff” method). The background process was blocking on trying to read a field from Main, and only unblocking when QApplication finally exited.

You should not expose partially constructed objects like this. It’s a bad idea, the VM will hate you for it and it will punish you with incomprehensible bugs as a result.

Edit: In fact, it turns out that this isn’t about partially constructed objects. It’s about static initialization. Because the initialization of the Main$ class depends on the construction of the Main object, Main$ will not be fully initialized until the constructor has exited. The JVM semantics guarantee that class loading is single threaded and that other threads cannot access the class until it has finished loading. Hence the observed problem.

This entry was posted in programming and tagged , on by .