Concurrent Programming using Threads

Concurrent Programming using Threads - Java Tutorial Java...

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

View Full Document Right Arrow Icon
Java Tutorial Java Threads java-08.fm Greg Lavender Slide 1 of 12 6/17/99 Concurrent Programming using Threads Threads are a control mechanism that enable you to write concurrent programs. You can think of a thread in an object-oriented language as a special kind of “system object” that contains information about the state of execution of a sequence of function calls that are said to “execute as a thread”. Usually, a special “run” or “start” procedure starts a separate thread of control. Normally, when you call a function or procedure, the compiler sets-up a stack frame (also called an activation frame) on the run-time procedure call stack, pushes arguments (or puts them into registers), and calls the function. The stack is also used as temporary storage for locally allocated objects declared in the scope of a procedure. In a sequential program, there is only one run-time stack and all activation frames are allocated in a nested fashion on the same run-time stack, corresponding to each nested procedure call. In a multi- threaded application, each “thread” represents a separate run-time stack, so you can have multiple procedure call chains running at the same time, possibly on multiple processors. Java on Solaris supports multi-processor threads. In a sequential program, the main run-time stack is allocated at program start and all procedure calls, including the initial call to “main” are made on this single run-time stack. In a multi-threaded program, a program starts on the system run-time stack where the main procedures runs. Any functions/procedures called by the main procedure have their activation frames allocated on this run-time stack. If the main procedure creates a new thread for run some procedure (usually calling a special “thread creation or construction” procedure/method), then a new run-time stack is dynamically allocated from the heap and the activation frames for the procedures are allocated on this new stack.
Background image of page 1

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

View Full DocumentRight Arrow Icon
Java Threads java-08.fm Greg Lavender Slide 2 of 12 6/17/99 Thread Stacks Question: How large should the heap allocated thread stack be? A thread stack will contain the activation record of the “starting” thread procedure (e.g., called the “run” method in Java), as well as any procedures that are called by the procedure that was first started in the new thread of control. So, the thread stack needs to be large enough to hold the maximum number of bytes required to hold all the activation records of the deepest procedure call c hain, as well as storage for all local variables allocated on the stack. Main thread and run-time stack Stack ... Multiple thread run-time stacks, each a separate “thread of execution” main() { thread_1 thread_2() thread_n() f() g() h() run() run() run() for(i=0; i< n; i++) thread_create(. ..) ... } Activation
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/16/2009 for the course SYSC 3303 taught by Professor Shramp during the Spring '09 term at Carleton CA.

Page1 / 12

Concurrent Programming using Threads - Java Tutorial Java...

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