Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Programmer Certification (OCPJP)
Threads Notes for Exams
Nik Arora
Ranch Hand
Posts: 652
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi All,
Can anybody send me the notes that is reuired to prepare for threads or the points and also some questions
Thanks All
Meena R. Krishnan
Ranch Hand
Posts: 178
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
..some of the notes till what I have gone throug so far:
Threads: [B]Objective 1: Write code to define, instantiate, and start new threads using both java.lang.Thread and java.lang.Runnable. [/B] A thread can be defined and instantiated in one of two ways: case 1. by extending java.lang.Thread (or) class TestThread extends java.lang.Thread{ public void run(){ //the job that needs to be run goes here. } } case 2. by implementing java.lang.Runnable interface. class TestRunnable implements java.lang.Runnable{ public void run(){ //job goes here. } } It is better use Runnable so that your class can extend other classes, if it needs to. Instantiating ,starting and running a new thread ------------------------------------------------ for case 1: TestThread t = new TestThread(); t.start(); for case2: TestRunnable r = new TestRunnable(); Thread t = new Thread(r); t.start(); //still use thread obj to call the start() start() will in turn call the run() method. Calling run() directly in the code will not start a new thread or create a new call stack. Instead, it will be just a method call. Thread Name: ----------- Use setName() / getName() to set and get the name of the thread. With the call to start(), a new thread of execution starts and a new call stack will be created for that thread. Once a thread has been started, you can never call start() again for that thread, doing so will result in IllegalThreadStateException. Also, once a thread is in 'dead' state, calling start() on that thread will cause runtime exception. When there are more than one threads, the thread scheduler decides which thread in the runnable state to run next. Some thread class methods that can influence the scheduler are: sleep() ,yield(), setPriority() and join() Thread-related methods from Object: wait() , notify() and notifyAll() all these are public final methods. Thread States: -------------- [B]Objective 2:Recognize the states in which a thread can exist, and identify ways in which a thread can transition from one state to another.[/B] * Once a thread instance has been created and before the call to start(). The thread is in 'new' state but not yet 'alive'. * Once the method start() is called, the thread moves from 'new' to runnable state and the thread is said to 'alive'. * Once run() is called, the thread moves from runnable' to running' state. * while the thread is in a running state, it can also go into 'waiting' or 'sleeping' or 'blocked' state. * Once run() completes, the thread moves from runnable to 'dead' state. sleeping: --------- public static void sleep(long milliseconds); A running thread can be slowed down by calling the Thread class' static method sleep(). This method can throw checked InterruptedException when its sleep gets interrupted by another thread. Therefore enclose it in try ..catch and handle or declare the error. try{ Thread.sleep(1000); //1 second }catch(InterruptedException i){} It is a static method and calling it from anywhere will cause the currently running thread to go into sleep. One thread cannot make another thread to sleep but can only make the currently running thread to go into sleep. Use this method to give all threads a chance to run. Once the sleep() expires, the thread won't go into running state immediately but only go into [I]runnable[/I] state and wait until its next turn planned by the scheduler. This method is guaranteed to put the thread into sleep. Yielding --------- public static void yield() This makes the currently running thread to go back into [I]runnable[/I] state so that other threads of same priority will have a chance to run. This method is not guaranteed to do what it is supposed to all the time. Priority -------- The priority of a thread can be set using public static void setPriority(n) where n is a number from 1 to 10 , 10 being the highest priority. You can get the priority of a thread using getPriority(); At any time the currently running thread will have the same or higher priority than the other threads. Joining ------- public final void join(Thread t) this makes the current thread join onto the end of the given thread t. In other words, it will block the current thread to go into [I]runnable[/I] state until after the other thread t is no longer alive. You can also use overloaded constructor that takes a time duration, which says the current thread to stop waiting after so many milliseconds even if the other thread is not complete. This method is guaranteed to make the current thread stop execution until the other thread completes. More on Thread states: --------------------- A thread can go into a running state only from a runnable state. A thread can go into a runnable state from any of these : [I]'new' , 'waiting/blocking/sleeping' , running[/I] start() --> 'new' to runnable sleep() / join() /yield --> running --> runnable join() --> can make the currently running thread to go into runnable state until the other thread to join on ends. Synchronization: ---------------- [B]Objective 3:# Given a scenario, write code that makes appropriate use of object locking to protect static or instance variables from concurrent access problems.[/B] Synchronization prevents multiple threads from accessing/updating the same data at the same time. *Every object has [B]a[/B] lock. *A thread must acquire this lock before entering a synchronized section. *If one thread owns the lock, all other threads trying to enter the same section must wait. *If a thread goes to sleep, it doesnot release any lock that it holds. *Only methods and block of code can be synchronized. eg. public synchronized void updateAmount(){} synchronized(this){ ...lots of code here } * A class can have both synchronized and non-synchronized methods. *In the case of an instance method, the lock is for the 'this', the current object and in the case of a static method, it is for the class (java.lang.Class). *A thread is said to 'blocked' when the lock it tries to get is already taken. * Two threads invoked using the same instance will block each other while trying to acquire the object's lock. * Two threads calling static methods on the same class will block each other. * A thread calling a static synchronized method and another thread calling non-static synchronized method will not block each other, as one is invoked on the object and the other the Class. *Also threads synchronized on the same object, will block each other. synchronized(object){ } *As a better practice, static fields should be accessed only from static synchronized methods and non-static fields from non-static synchronized fields. Since static and non-static synchronized methods do not block each other, they both can access the same field at the same time. * Thread-safe class is the one in which each individual method is synchronized. *Thread deadlock can happen when two threads are blocked waiting to acquire lock on the other. Thread Iteraction: ------------------ [B]Objective 4:Given a scenario, write code that makes appropriate use of wait, notify, or notifyAll.[/B] * wait, notify and notifyAll which are Object's methods can only be called from within a synchronized method or block. *A thread must own an object's lock in order to invoke wait() or notify() on that object. * Every object can have a list of threads waiting for its lock. * A thread can call wait() on the target object in order to get into its waiting list. * when wait() is invoked on an object, the currently executing thread who owns the object's lock will give up its lock immediately. * notify() is called to notify a waiting thread. It cannot specify which waiting thread to notify. * unlike wait(), invoking notify() does not release the object's lock immediately. The lock will be released only after the synchronized code is completed. * notifyAll() is invoked to notify all the waiting threads.
[ May 10, 2007: Message edited by: M Krishnan ]
[ May 18, 2007: Message edited by: M Krishnan ]
Nik Arora
Ranch Hand
Posts: 652
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Krishnan,
Thanks for the notes.
Apna Apana
Greenhorn
Posts: 20
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can you please explain abt Synchronization and
thread
communication.
Apna Apana
Greenhorn
Posts: 20
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thank You Mr.Krishnan
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
SCJP - Best Material for Focussed Preparation
Threads Problem
Cleared SCJP with 93%.
new SCJP
Cleared SCJP
More...