1 / 17

Web Based Programming Section 8

Web Based Programming Section 8. James King 12 August 2003. 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. Multi-Threading.

gaenor
Download Presentation

Web Based Programming Section 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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.

  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

  5. Multiple threads of executionThread 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

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

  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

  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(); } }

  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

  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(); } }

  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

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

  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(); }

  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

  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); }

  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();

  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

More Related