Skip to content

Instantly share code, notes, and snippets.

@kineticz
kineticz / Tail.java
Created January 29, 2015 23:09
Print the tail of a file, finally got it right.
package je3.io;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* Created by IDEA on 29/01/15.
*/
public class Tail {
public static String[] tailLines(String filename, int nLinesToRead) throws IOException {
@kineticz
kineticz / WalkDir.java
Created January 31, 2015 21:34
Why do I get a nul pointer exception? From what I see from the debugger, `File d` is not null.
package je3.io;
import java.io.File;
import java.util.List;
/**
* Created by IDEA on 31/01/15.
*/
public class WalkDir {
private List<File> recursiveList;
@kineticz
kineticz / WalkDir.java
Created January 31, 2015 21:40
Correct version of directory walker.
package je3.io;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IDEA on 31/01/15.
*/
public class WalkDir {
@kineticz
kineticz / DirSummariser.java
Created January 31, 2015 22:16
How do you translate this into java 8 (functional style)?
public long calculateDirSize() {
for(File f : dirWalker.getRecursiveList()) {
dirSize += f.length();
}
return dirSize;
}
@kineticz
kineticz / ThreadSafeIntList.java
Created February 1, 2015 13:26
Why should we `synchronized(original)` since `original` is already thread safe?
package je3.thread;
/**
* Created by IDEA on 01/02/15.
*/
public class ThreadSafeIntList {
protected int[] data;
protected int size = 0;
private static final int default_capacity = 8;
@kineticz
kineticz / ThreadLister.java
Created February 1, 2015 13:48
How does g.enumerate() know the difference between threads and groups?
private static void printGroupInfo(PrintWriter tou, ThreadGroup g, String indent) {
if(g == null) return;
int num_threads = g.activeCount();
int num_groups = g.activeGroupCount();
Thread[] threads = new Thread[num_threads];
ThreadGroup[] groups = new ThreadGroup[num_groups];
// java.lang.ThreadGroup
// public int enumerate(ThreadGroup[] list, boolean recurse)
// Copies into the specified array references to every active subgroup in this thread group. If recurse is true, this method recursively enumerates all subgroups of this thread group and references to every active thread group in these subgroups are also included.
g.enumerate(threads, false);
@kineticz
kineticz / PrintChar.java
Last active August 29, 2015 14:14
why aren't the x's printed when PrintNum reaches 50?
package je3.thread;
/**
* Created by IDEA on 01/02/15.
*/
public class PrintChar implements Runnable {
private char charToPrint;
private int times;
@kineticz
kineticz / LinePane.java
Created February 3, 2015 11:22
Is it ok not to call constructor of super class?
package je3.thread;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
/**
* Created by IDEA on 03/02/15.
*/
public class LinePane extends Pane {
@kineticz
kineticz / ShowText.java
Created February 3, 2015 11:42
How can I anchor text at the center?
package je3.thread;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
package je3.thread;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;