Jan 27, 2009

Syntax Highlighting for WordPress

  • My conversion from an online code formatting tool, that required me to copy and past and change settings, to the WordPress plug-in WP-Syntax has been like taking a blindfold off. I can actually see and read my code on my blog now! I love it!!!!! Life is soo much better when it is easier to read code. :-)

  • Simple Java Thread Example

    I decided to take a quick break from homework and write this simple thread example for my system integration lab project
    This code creates 3 threads that count. 1 thread counts to 2 million while the second 2 threads are set to split up the work. After all counting is done a summary of how long each took and what the time savings was.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    package threadExample;

    import javax.swing.JOptionPane;

    public class CountingThreadExample {

    /**
    * Thread example created by @author Mike on 10/30/08
    */

    public static void main(String[] args) {
    //2,000,000,000
    Counter counter1 = new Counter("Counter 1");
    Thread thread1 = new Thread(counter1);
    Counter counter2 = new Counter(1000000000, "counter 2");
    Thread thread2 = new Thread(counter2);
    Counter counter3 = new Counter(1000000000, "counter 3");
    Thread thread3 = new Thread(counter3);
    thread2.start();
    thread3.start();
    thread1.start();
    while (counter1.getRunTime() == 0) {
    try {
    Thread.sleep(1000);
    }
    catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    long time1 = counter1.getRunTime();
    long time2 = counter2.getStartTime();
    long time3 = counter3.getEndTime();
    long splitWork = time3 - time2;
    long faster = time1 - splitWork;
    JOptionPane.showMessageDialog(null,
    "A single thread counted from 0 - 2,000,000,000 in "
    + time1 + " milliseconds.\n" +
    "Two threads that split the work did it in " +
    splitWork + " Milliseconds.\n" + "That is " + faster
    + " milliseconds faster!\n" +
    "If you have a multi-core processor and have a way to monitor\n"
    + "the usage, run this program again and watch each core.\n"
    + "1000 milliseconds = 1 second");
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package threadExample;

    public class Counter implements Runnable{
    double max = 2000000000;
    String name = "No name";
    long runTime = 0;
    long startTime = 0;
    long endTime = 0;
    public Counter() {
    }
    public Counter(String name) {
    this.name = name;
    }
    public Counter(double max, String name) {
    this.max = max;
    this.name = name;
    }
    public void run() {
    startTime = System.currentTimeMillis();
    for (double i = 0.0; i <= max; i++) {
    // Does nothing, just counts.
    }
    endTime = System.currentTimeMillis();
    runTime = endTime - startTime;


    }

    public long getRunTime() {
    return runTime;
    }
    public long getStartTime() {
    return startTime;
    }
    public long getEndTime() {
    return endTime;
    }
    }


  • Java Reflection

    Java reflection rocks!!!! Being able to create a class object from a string is cool. Mix that with an interface and you can create objects based on string and make function calls that are defined in the interface without ever knowing what the object is or what methods it has, as long as the object implements the said interface. I created an example of reflection and interface use in the same code.

    The example takes place in a dance performance hall. A man object and a woman object is created which both are dancers because they both implement the dancer interface. The man and the woman are cast as dancers and then told to dance. No need to know all of their methods since I already know what dancers can do because of the interface. Now all that’s left to do is tell the dancers what to do.
    Run() Jump() Trip()! :-)

    All the classes are in my sandbox package. In order to create an object from text using reflection, the path to the class has to be known so what package I put the classes in is important but I only show it in the StartDance class code.

    1. public interface Dancer {
    2. public void jump();
    3. public void run();
    4. public void trip();
    5. public void rollCall();
    6. }
    1. public class Man implements Dancer{
    2. public void jump() {
    3. System.out.println("I jumped. Grrrr!");
    4. }
    5. public void run() {
    6. System.out.println("I ran. Grrrr!");
    7. }
    8. public void trip() {
    9. System.out.println("I triped. Grrrr!");
    10. }
    11. public void rollCall() {
    12. System.out.println("I am a Man. Grrrr");
    13. }
    14. public void goToWork() {
    15. System.out.println("I'm off to work. Grrrr");
    16. }
    17. }
    1. public class Woman implements Dancer{
    2. public void jump() {
    3. System.out.println("I jumped. Tehe!");
    4. }
    5. public void run() {
    6. System.out.println("I ran. Tehe!");
    7. }
    8. public void trip() {
    9. System.out.println("I triped. Tehe!");
    10. }
    11. public void rollCall() {
    12. System.out.println("I am a Woman. Hear me Roar!");
    13. }
    14. public void takeCareKids() {
    15. System.out.println("Kids are soo cute. Tehe");
    16. }
    17. }
    1. package sandbox;

    2. public class StartDance {

    3. @SuppressWarnings("unchecked")
    4. public static void main(String[] args) {
    5. String command1 = "sandbox.Man";
    6. String command2 = "sandbox.Woman";

    7. try {
    8. Class class1 = Class.forName(command1);
    9. Class class2 = Class.forName(command2);
    10. Dancer dancer1 = (Dancer)class1.newInstance();
    11. Dancer dancer2 = (Dancer)class2.newInstance();
    12. dancer1.run();
    13. dancer1.jump();
    14. dancer2.trip();
    15. dancer2.rollCall();
    16. }
    17. catch (Exception e) {
    18. e.printStackTrace();
    19. }
    20. }
    21. }

No comments:

Post a Comment