Anda di halaman 1dari 11

Computer Architecture

Multi-Thread in Java

Hongfu Huang; Cheng Qi 15.12.2006

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 those who have no or little knowledege in Java Language


JVM concepts Multiple Thread Concepts Performance monitoring

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

Architecture of J2SE 5.0 Monitoring and Management Support.

Java Threads
Language-level support threads implementation
Compare with C++ or SmallTalk.

JVM specification do not specify how to implement thread scheduling


Generally JVM will work togerther with OS for multi-thread scheduling. So there are two levels:User level and Kernel level

Two Basic Classes


Runnable (interface)
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

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"); } }

JConsole Examples (Linux)

JConsole Examples (Linux)

Q&A
My suggestion for Computer Programming:

Always remember the concepts you learn at your


university(programing language and related platform change in a very quickly speed but the concepts are relative stable)

If possible,do not use IDE when you begin to study


a programming language.(IDE tools can improve your programming efficency but at the same time they hide so many details)

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/

Anda mungkin juga menyukai