Java Multi Threading 1
Java Multi Threading 1
Multi-Thread in Java
Outline
Goals OS Concepts(reference CA lecture materials) JVM Java Threads Q&A
Goals
For those who have learned Java Language
How to monitoring your java application
For Both
better understand the concepts in leture: Computer Architecture
JVM -- Concepts
JVM is a software, a program, a process with multi-thread. The input of JVM is JAVA program(or application,it composited by some bytecode files, or java class file). When runing a JAVA program(or an application.Be careful here! We do not call a process here.),how many processes are activated in memory? Only one, JVM!!!
JVM -- Concepts(cont.)
When runing a JAVA program, how many threads are active in memory?
It depends on JVM implementation and the functions of JVM used by the customers program. The customers program can create threads
JVM --Monitoring
How to monitor a JVM process. --under Window OS, using Windows TaskManager. Or What is running --under Linux or Unix OS, using top, pstree or ps command. How to monitor Java Threads? --JMX(Java Management Extension Specification)
Linux Process
Linux Threads
Windows Threads
JVM -- Monitoring(cont.)
Since JDK1.5, JConsole is available for JVM monitoring.
The jconsole executable is in JDK_HOME/bin, where JDK_HOME is the directory where the JDK is installed. Local Monitoring Remote Monitoring
There are also some other commercial tools for JVM monitoring.
Please search them by google
Java Threads
Language-level support threads implementation
Compare with C++ or SmallTalk.
Thread (Class)
A thread is a thread of execution in a program. JRE provide the Class Thread, which implement the Runnable interface.
Examples
Implementation of Runnable
class PrimeRun implements Runnable { long minPrime; PrimeRun(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime
System.out.println(Thread.currentThread().toString() +" ID:" + Thread.currentThread().getId());
} public static void main(String args[]) { PrimeRun worker1 = new PrimeRun(1); Thread athread = new Thread(worker1,worker) athread.start();
System.out.println(Thread.currentThread().toString() +" ID:" + Thread.currentThread().getId());
} }
Examples (cont)
subclass the class Thread
class PrimeThread extends Thread { long minPrime; PrimeThread(long minPrime) { this.minPrime = minPrime; } public void run() { // compute primes larger than minPrime
System.out.println(toString() +" ID:" + getId());
} public static void main(String args[]) { PrimeThread worker1 = new PrimeThread(1); worker1.setPriority(5); worker1.start(); Thread.currentThread().setPriority(3);
System.out.println(Thread.currentThread().toString() +" ID:" + Thread.currentThread().getId());
} }
Examples (cont)
public class SimpleThread extends Thread { private int countDown = 5; private static int threadCount = 0; private int threadNumber = ++threadCount; public SimpleThread() { System.out.println("Making " + threadNumber); } public void run() { while(true) { System.out.println("Thread " + threadNumber + "(" + countDown + ")"); //if(--countDown == 0) return; } } public static void main(String[] args) { for(int i = 0; i < 1; i++) new SimpleThread().start(); System.out.println("All Threads Started"); } }
Q&A
My suggestion for Computer Programming:
Reference List
Computer Architecture: http://www.fb9dv.uniduisburg.de/vs/en/education/dv3/index2006.htm Using JConsole to Monitor Applications: http://java.sun.com/developer/technicalArticles/J2SE/jco nsole.html HotSpot Thread Implementation (Solaris): http://java.sun.com/docs/hotspot/threads/threads.html Java VM Specification: http://java.sun.com/docs/books/vmspec/