Javathreads - THREADS • A thread or Light Weight...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
THREADS A thread, or Light Weight Process (LWP), is a basic unit of CPU utilization and consists of a program counter (PC), register set, and a stack space. It a flow of control within a program. Multiple threads within an application share the same address space, i.e., the code section and data section. Each thread has its own stack space. Threads share instance variables but not local variables. A Java language object represents the thread itself and an arbitrary target object contains the method the thread is to execute. A new thread is born when an instance of the java.lang.Thread class is created. The thread object represents a real thread. A class must contain a run() method and state that it implements the Runnable interface. An instance of this class is a runnable object and can serve as the target of a new Thread. e.g. class Animation implements Runnable { Thread mythread; Animation() { //constructor mythread = new Thread (this); mythread.start(); } public void run() { while(true) { …. } } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Then an instance of Animation is created with: Animation happy = new Animation(); When the constructor runs, it creates a Thread object, mythread, with an instance of Animation as its target object. Then mythread.start() calls run() method of the target object,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/29/2011 for the course CNT 6707 taught by Professor Ahuja during the Spring '11 term at UNF.

Page1 / 5

Javathreads - THREADS • A thread or Light Weight...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online