woensdag 13 april 2011

[Java] Start Amount of Threads depending on Available Cores

Parallel threading depending on Amount of Cores

Here's a small snippet to keep in mind when you're programming and you've got to do some parallel computing.


static void test() {
int n = Runtime.getRuntime().availableProcessors();
System.out.println(n);
for (int i=0; i<n; i++) {
Thread(new Runner()).start();
}
}

It's not recommended to start your threads like this (I'd suggest an ExecutorService for it), but it's something to remember ;)

Qkyrie

[Java] Execute a terminal-command in Java

Here's an easy way to implement command calls in Java. The program prints the response from the CLI.


import java.io.*;
  1. public class TestExec {
  2. public static void main(String[] args) {
  3. try {
  4. Process p = Runtime.getRuntime().exec("cmd /C dir");
  5. BufferedReader in = new BufferedReader(
  6. new InputStreamReader(p.getInputStream()));
  7. String line = null;
  8. while ((line = in.readLine()) != null) {
  9. System.out.println(line);
  10. }
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. }
  15. }

Qkyrie

dinsdag 12 april 2011

Web Designers vs Web Developers - Infograph

Totally had a big laugh when I saw this one. Especially for us, coders, this is really, like, striking.

Web Designers vs Web Developers is brought to you by Wix.com
Use creative design to make a Free Website
You are most welcome to share this infographic with your audience.

vrijdag 8 april 2011

[Java] Format a Float @ 2 decimals precision

Here's a small code example of how you could format a float to be displayed at 2 decimals precision (only when necessary).

  1. import java.text.NumberFormat;
  2. ...
  3. NumberFormat nf = NumberFormat.getInstance();
  4. nf.setMaximumFractionDigits(2);
  5. nf.setMinimumFractionDigits(0)

To actually format a float, one could simply do:

  1. nf.format(myFloat);

It's as simple as that ;)

woensdag 6 april 2011

[Java] Resizing a BufferedImage

Here's a small piece of code that might aid you in resizing a BufferedImage. Most methods I saw on the internet just took the BufferedImage and resized it to fit the screen. This method will return a new bufferedImage with the new width and height. 





  1. public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
  2.         int w = img.getWidth();  
  3.         int h = img.getHeight();  
  4.         BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());  
  5.         Graphics2D g = dimg.createGraphics();  
  6.         g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,     RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
  7.         g.drawImage(img, 00, newW, newH, 00, w, h, null);  
  8.         g.dispose();  
  9.         return dimg;  
  10.     }  

maandag 4 april 2011

New Project - EmailCrawler in JavaFX With JScrape.

This week I decided to port my gui from my emailcrawler I had written before
(In pure Java)  to JavaFX.

Unfortunately, I wasn't satisfied with the code I had written over a year ago.
I easily discovered that, a year ago, I wasn't able to write a single class without
making obvious epic failures.

That's why I decided to use my JScrape framework to create an entirely new project.


Release 1 is soon to come. I reckon by friday.

I'll keep in touch

Qkyrie

vrijdag 1 april 2011

Performance Showdown - Map<K,V> vs. List<T> Java

Time for another showdown. This time, it's the Java Map Interface vs the List Interface. In what cases are Maps faster than Lists?

Accessing complexity


Actually, if you don't want to entirely read this article, you should know that in almost all cases accessing a list is faster.

When you're accessing a hashmap, you'll first create a hash representation of your key, get to the right "bucket" and then get the value from an implemented list, whereas accessing a value from an ArrayList will merely fetch the value from the list.

After this fact, you might not want to use Maps, but don't worry, there's more..

Two Entirely Different Datastructures


You should never forget that both Map and List are two entirely different datastructures, each with its own advantages.

For example, the keys for a Map aren't linear. Anything can be the key, which becomes very handy when dealing with Strings as keys.

Remember this:
If you need a List, use a list. If you need a Map, use a Map. If at any time you can pick one of both data structures - for example when dealing with consecutive Integers as keys - you're probably better off picking the ArrayList implementation.

But there's more
Remember ArrayList isn't the only List implementation! Choosing the right interface is a first step to choosing the right datastructure, but sometimes a lot of performance can be gained by using another implementation of the chosen interface.

If you frequently add elements to the beginning of a List or iterate over the List to delete elements from its interior, you should consider using LinkedList.

Lists: ArrayLists, LinkedList, Vector (synchronised list)
Maps: HashMap, TreeMap, LinkedHashMap

Finally, there's also a Set interface, used when you don't need access by key, and want to use methods like "contains()"

Sets: HashSet, TreeSet, LinkedHashSet


Comments
If you'd like to add something to this, or you want to correct me on something, feel free to leave a comment below or on my twitterpage!


@Qkyrie and David