Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Web Based Programming Section 8 James King 12 August 2003.

Similar presentations


Presentation on theme: "1 Web Based Programming Section 8 James King 12 August 2003."— Presentation transcript:

1

2 1 Web Based Programming Section 8 James King 12 August 2003

3 2 Topics Object oriented concepts How Java programs work First steps Importing classes Graphical classes Applets Input and Output Streams Multiple threads of execution Network programming

4 3 Multi-Threading Most network servers (and other types of programs) handle several clients at the same time. This requires doing several things concurrently. In the past this meant running multiple copies of the program with the processor quickly swapping between them Modern Systems allow a single program to perform multiple tasks concurrently with the processor switching between tasks. Each of these separately executable parts of the program is called a Thread.

5 4 Multiple threads of execution – Terminology A process is the code and data of a program (in our case the program is split into objects) A thread runs inside a process executing the code and read and writing the data (it has its own program counter and register contents) All the threads inside a process see the same code and share the same data A Java program can have many threads in side the same program running at the same time all performing different (or the same) tasks

6 5 Multiple threads of execution Thread Problems Multiple threads sharing the same data can cause inconsistency problems because each thread can overwrite the changes written by another thread. Imagine two threads both updating a shared (static) variable they both simple want to increase it by one. The variable starts at 0

7 6 Thread Problems shared variable value 0 1.Reads shared variable (reads the value 0) 3.adds one to the value it read (result 1) 4.writes the result (1) back to the shared variable 5.adds one to the value it read (result 1) 6.writes the result (1) back to the shared variable 2.Reads shared variable (reads the value 0) Thread 2Thread 1 shared variable value 1

8 7 How to make a Thread - inheritance Your class can extend the class Thread and provide a run method Create an instance of the class Call start() to start the run method in a independent thread

9 8 How to make a thread using inheritance public class monster extends Thread { public void run() { for (int i=0;i<10;i++) { System.out.println("growl"); } } public static void main (String args[]) { monster snotty=new monster(); monster grotty=new monster(); snotty.start(); grotty.start(); }

10 9 Making a thread using Interfaces If your class already extends another class you can implement the Runnable interface instead You still provide a run method to start the thread first you have to create a thread around your class and then you can start() it

11 10 Making a thread using Interfaces public class monster2 implements Runnable { public void run() { for (int i=0;i<10;i++) { System.out.println("growl"); } } public static void main (String args[]) { monster2 snotty=new monster2(); monster2 grotty=new monster2(); Thread m1=new Thread(snotty); Thread m2=new Thread(grotty); m1.start(); m2.start(); }

12 11 Stopping a Thread Once a thread has performed its task it should be stopped However Java has removed the methods to suspend and stop a thread The only safe way to stop a thread is for the run method to exit

13 12 Stopping a Thread boolean exit=false; public void finish() { exit=true; } public void run() { while(exit==false) { System.out.println(myname+": growl"); } }

14 13 Stopping a Thread public static void main (String args[]) { stop snotty=new stop(); stop grotty=new stop(); snotty.start(); grotty.start(); try { Thread.sleep(10000);} catch(Exception e) {} snotty.finish(); grotty.finish(); }

15 14 Waiting for a Thread – Busy Waiting You can wait for a thread to finish by using isAlive() while snotty.isAlive() { } However this is called busy waiting because the loop is always executing as fast as possible and using up a lot processing power

16 15 Waiting for a thread - Polling You could add a sleep (which does not take up processing power). However this is still inefficient because the loop will still execute many times This is called polling – checking every so often – in this case every 100 milli seconds While snotty.isAlive() { Thread.sleep(100); }

17 16 Waiting for a Thread - join Join makes your thread block until the other thread finishes. Join does not busy wait or poll and does not use up processing power It is the best solution to the problem snotty.join();

18 17 Thread Priorities In some cases some threads are more important than others and you may want these to receive a larger share of the available processing power The Java reference documentation is a little sketchy about thread priorities


Download ppt "1 Web Based Programming Section 8 James King 12 August 2003."

Similar presentations


Ads by Google