next next up down toc Memos schedule allInOne PDF PDF mail
all, section 12.3.

12.3.  Creating and Using

Why two different ways?

public interface Runnable 	{
	public abstract void run();
}

The first example:

Src/11/Thread_1.java.minusSTART_STOP



public class Thread_1 extends Thread    {

        private String info;
        int x = 0;

        public Thread_1 (String info) {
                this.info = info;
        }

        public void run () {
                x=1;
                System.out.print(info);
        }

        public static void main (String args []) {
                if (args != null)
                for (int n = 0; n < args.length; ++ n)  {
                        Thread_1 aT1  = new Thread_1(args[n]);
                        if ( n % 2 == 0 )
                                aT1.setPriority(Thread.MIN_PRIORITY);
                        aT1.start();
                }
        }
}

Source Code: Src/11/Thread_1.java

Result:

% java Thread_1 a b c d e f g h i j k l m n o p q r s 
bdfhjlnpracegikmoqs% 

Second Example:

Src/11/Thread_2.java.minusSTART_STOP



public class Thread_2 extends Thread    {

        private String info;

        public Thread_2 (String info) {
                this.info = info;
        }

        public void run () {
                long sleep = (int)(Math.random() * 10000);
                System.out.println(info + " sleeps for " + sleep );
                try {
                        sleep(sleep);
                }
                catch (  InterruptedException e ) {
                        e.getMessage();
                }
        }

        public static void main (String args []) {
                int count = 0;
                if (args != null)
                for (int n = 0; n < args.length; ++ n)  {
                        Thread_2 aT1  = new Thread_2(args[n]);
                        if ( n % 2 == 0 )
                                aT1.setPriority(Thread.MIN_PRIORITY);
                        aT1.start();
                }
                while ( count != 1 )    {
                        try {
                                count = activeCount();
                                System.out.println("activeCount() = " +
                                        count );
                                sleep(500);
                        }
                        catch (  InterruptedException e ) {
                                e.getMessage();
                        }
                }
                
        }
}

Source Code: Src/11/Thread_2.java

java  Thread_2 a b c d
activeCount() = 5
b sleeps for 1063
d sleeps for 8295
a sleeps for 2197
c sleeps for 2619
activeCount() = 5
activeCount() = 5
activeCount() = 4
activeCount() = 4
activeCount() = 3
activeCount() = 2
 ..
activeCount() = 2
activeCount() = 1

The execution of the Java Virtual Machine ends once most threads reach the end of their run() methods. ``Most'' means that there are user and daemon threads and thread groups; execution of the JVM ends when there are no more user threads. The distinction is made by calling setDaemon() ; the distinction is necessary, because threads that deliver events and manage painting in a window system are irrelevant for deciding if an application has completed it's job


back next up down toc Memos schedule allInOne pdf PDF mail

Created by unroff, java2html & & hp-tools. © by csfac. All Rights Reserved (2010).
It is not allowed to print these pages on a CAST printer.
Last modified: 01/April/10 (17:16)