Threads Fall 2005 IST 311 Threads A thread
- Slides: 23
Threads Fall 2005 IST 311
Threads A thread is a single sequence of executable statements within a program – Java applications, flow begins with first statement in main( ) – Java applets, flow control begins with first statement in init( ) CPU executes one instruction at a time Multiple threads executing concurrently and rapidly gives the appears that the CPU is running more than one task at a time
Threads Multitasking – technique of concurrently executing several tasks within a program – Downloading an image from the internet while running a speadsheet calculation Multitasking requires use of separate thread for each task
Threads CPU uses fetch-execute cycle to retrieve next instruction from memory and execute it Multithreaded programs make it possible to divide up the CPU’s time and share it among the threads CPU’s schedule is managed by a scheduling algorithm – Controlled by the operating system and JVM
Threads Thread scheduling is handled differently on Unix, Windows, and Macintosh systems One common scheduling technique is known as time slicing – Each thread alternatively gets a slice of CPU time – System gives each thread a quantum of CPU time to execute instructions – When quantum expires, thread would be preempted another thread given a chance to run – On a 300 -megahertz CPU, a thread could execute 300, 000 machine instructions during a millisecond
Threads Priority scheduling – higher priority threads are allowed to run to completion before lower-priority threads – High-priority thread might be keyboard input from the user so the user doesn’t experience noticeable delays
Threads
Threads How to create a multithreaded program: – Implement the Runnable interface for an existing class by implementing the void run( ) method – Create several Thread instances by first creating instances of the Runnable class and passing each as an argument to the Thread( ) constructor – For each thread instance, start it by invoking the start( ) method on it.
Threads Number. Printer and Number program
Threads start( ) and stop( ) methods used to control thread’s execution – Sometimes called automatically – An applet is treated as a thread by a browser, which is responsible for starting and stopping it set. Priority( int) lets you set a thread’s priority set. Priority(Thread. MAX_PRIORITY);
Threads Thread implementation in Java is platform dependent – Test to ensure program will perform correctly on a given platform Coordinate behavior of 2 threads by giving one a higher priority High-priority thread that never gives up CPU time can starve lower-priority threads
Threads Thread. sleep( ) method causes the thread to yield and not be scheduled until a certain amount of real time has passed – Halt a running thread for a given number of milliseconds – Throws an Interrupted. Exception which is a checked exception – sleep( ) must be embedded within a try/ catch block or the method it is in must throw an Interrupted. Exception
Threads Thread. yield( ) method causes the thread to yield CPU, allowing the thread scheduler to choose another thread
Threads are asynchronous – The order of execution is sporadic and unpredictable from programmer’s point of view – Impossible for a programmer to predict when and for how long an individual thread will run
Thread States and Life Cycle State Ready Running Waiting Description The thread is ready to run and waiting for the CPU (queued) The thread is executing on the CPU Blocked The thread is waiting for some event to happen The thread has been told to sleep for a time The thread is waiting for I/O to finish Dead The thread is terminated Sleeping
Part 2
Applets start( ) is called once when the applet is started. – Calls init( ) and paint( ) automatically and then calls run( ) to run the applet run( ) starts the applet running. – Called by start( ) stop( ) is called by the browser or appletviewer when the applet is quit
Applets init( ) is called automatically when the applet starts to perform initializations paint( ) paints the graphics on the applet. Called once at start and then can be called by repaint( ) when applet’s appearance is changed.
Calendar The class Calendar contains methods and constants to represent the date and time – Found in java. util. * – Constructors in Calendar are protected, so you must use get. Instance( ) method to return an instance of a Calendar subclass – May retrieve hour, minutes, seconds: int hours = time. get(Calendar. HOUR); int mins = time. get(Calendar. MINUTE); int secs = time. get(Calendar. SECOND);
Threads Applet to run a clock
Threads We override methods init( ), start( ), and stop( ) Web browser or appletviewer invokes these overridden methods Applet responds to controlling program With program like Clock, browser or viewer cannot regain control – stuck in an infinite loop
Threads Java is an implicitly threaded language – Several threads are started automatically every time you run a Java applet or application – For example, a separate thread will start for each Events generated by users (pressing buttons in a GUI) Automatic garbage collection Creation of a programmer’s own threads for running sections of a program independently
Threads To solve the Clock problem, we need to allow the applet to run in parallel with the browser – Give it a thread of its own – Will react to the mouse button Get into the habit of running an applet in its own thread
- Ist 311
- College of information sciences and technology
- Ist 311
- Ist 311
- Es ist herbst es ist herbst bunter blätter fliegen
- Winter kommt flocken fallen nieder
- Zu glauben ist schwer. nichts zu glauben ist unmöglich
- Winter kommt winter kommt flocken fallen nieder
- Es ist herbst es ist herbst bunter blätter fliegen
- 311
- Bcd addition of 184 and 576
- Art 294 a 311 novo cpc
- A2yes
- Sta 311 pdf
- Rpv 311
- Ise 311
- Ac 20-184
- Gc 311
- In 311
- Ssis 311
- Contoh knapsack problem
- Comp 311 textbook notes
- 311 k
- Traction grabber by doc