Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of.

Slides:



Advertisements
Similar presentations
Made with love, by Zachary Langley Applets The Graphics Presentation.
Advertisements

13/04/2015Client-server Programming1 Block 6: Threads 1 Jin Sa.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
1 Java - Threads A thread is an individual flow of control within a larger program. A program which is running more than one thread is said to be multithreaded.
Object-Oriented Software Engineering Concurrent Programming.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Universidad de Chile - Tupper 2007, Santiago - Fono/Fax: (56 2) cec.uchile.cl Módulo ECI - 11: Fundamentos de Redes de Computadores.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Concurrency Java Threads. Fundamentals Concurrency defines parallel activity Synchronization is necessary in order for parallel activities to share results.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
MultiThreaded Applications. What is Multithreaded Programming? Having your software appear to perform multiple tasks in parallel –Individual paths of.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads CSCE 190 – Java Instructor: Joel Gompert Wed, Aug 3, 2004.
1 Web Based Programming Section 8 James King 12 August 2003.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Chapter 6 (6.7 & 6.9 only) - Threads & Mutex Threads –A Thread is a basic unit of CPU utilization consisting of a program counter, register set and stack.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Multi-Threading in Java
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
Threads.
Java Applets.
Modern Programming Tools And Techniques-I
Multithreaded applets
Multithreading Lec 23.
Inheritance and Polymorphism
Java Programming Language
Java Applets.
Object-Orientated Analysis, Design and Programming
THREADS.
Interface.
Interfaces.
Java Applets.
A+ Computer Science METHODS.
Java Applets.
Java Based Techhnology
Multithreading.
Unit 1 Lab14 & Lab15.
Constructors, GUI’s(Using Swing) and ActionListner
A+ Computer Science METHODS.
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
Threads.
Gentle Introduction to Threads
Lecture 19 Threads CSE /6/2019.
CMSC 202 Threads.
What happens when many clients want to contact the server at the same time ? The iterative approach consist in “enqueuing” the requests (this is done.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Polymorphism Sometimes it is necessary to “see” an object of a certain class as being (also) of another: Polymorphism An object can be seen as being of the finally defined class or any one of the superclasses in the object hierarchy up to the Object class This makes that different class of objects may be considered from the same class But classes can extend only one other class. Sometimes it is necessary that classes which are extensions of different other classes look like the same class This can be achieved by interfaces

Inheritance Object GraphicObject RectangleObject CircleObject GraphicTest.java

The Drawing Methods drawLine(x1,y1,x2,y2) drawRect(x,y,w,h) fillRect(x,y,w,h) drawOval(x,y,w,h) fillOval(x,y,w,h) w x,y h w x,y h

The Drawing Methods drawPolygon(x[],y[],n) fillPolygon(x[],y[],n) setColor(color.colorname) colorname = black, blue, cyan, darkGray, gray, orange, white, red, pink, yellow setColor(new color(red,green,blue)) x[1],y[1] x[4],y[4] x[2],y[2] n = 5 x[3],y[3]

Drawing Strings and Changing the letters font drawString(str, x,y) We can change the standard Font by creating a new one Font afont = new font(“Arial”,font.Bold,20) c.setFont(afont); x This is the string y These Methods are the same used for painting over any window object of Java (Frame, Jframe, Panel, etc)

Interfaces Vektor Clock VisualVektor VisualClock VisualVektor INTERFACE: GraphicObjectInterface GraphicTestInterface.java

Reflection Reflection is the ability to “explore” an unknown class For example: Which are the variables of a class ? Which are the methods of a class ? Which are the declared methods ? Which are the declared variables ? What do I get if I perform a certain method of the class ?

The Most Important Methods Field [ ] getFields() Field [ ] getDeclaredFields() Method [ ] getMethods() Method [ ] getDeclaredMethods() Constructor [ ] getConstructors() Constructor[ ] getDeclaredConstructors() These are performed over an object of the class Class

How do I get a Class Object ? Class c = Classname.class; Console.class String.class etc.. Class c = object.getClass(); Console cs = new Console(); Class c = cs.getClass(); Class c = Class.forName(String); Class c = Class.forName(“Console”); ReflectionTest ReflectionTest2 ReflectionTest3

Objects doing things by themselves We will program a clock which updates itself (almost) every second For this, the clock must be of the class thread Every object of the thread class can start an independent execution line (“thread”) which will me executed in parallel to the programs of the main method

Making things in parallel The Thread Main Thread1 Thread2 Thread3

Making things in parallel The Thread A thread is an independent, parallel line of statements execution It consists basically of method (run) written in a special way which can be activated by another method When a method activates a thread it continues executing its own statements while the thread starts executing the statements of the method called run() There are different ways to write threads, we will see the most easy to write

Implementation of Threads One way (perhaps the most clear one) is to define a new class which is an extention of the Thread class and overwrite its run() method. Thread is an existing class This thread class has a run method (originally with no statements) which will be executed in parallel when activated. In order to execute this method a program must create an object of the extended class and apply the start() method to it. The definition of the new Thread class should look like this: public class MyThread extends Thread { And somewhere should appear: public void run() { //instrucction to be executed in parallel } NOTAS

Example public class SimpleThread extends Thread { public SimpleThread(String str) { super(str); } public void run() { for (int i = 0; i < 10; i++) { System.out.println(i + " " + getName()); try { this.sleep((int)(Math.random() * 1000)); } catch (InterruptedException e) {} System.out.println("DONE! " + getName()); The this.sleep(milisegundos) must appear in a try and catch block NOTAS

Use of this new class public class TwoThreadsTest { public static void main (String[] args) { SimpleThread t1,t2; t1 = SimpleThread("Jamaica"); t2 = SimpleThread("Fiji"); t1.start(); t2.start() } The start() triggers the execution of the run method of the thread. There are other methods which can be applied to a thread: suspend(), resume(), stop(). These are however deprecated because their use can easily lead to deadlocks NOTAS

The clock has a thread to advance the current time ActiveClock Two threads

Sometimes you cannot program the server as an extension of a thread For example, if the server has to implement a graphical interface it should be declared as an extension of a Frame, if it is programmed as an Applet it must extend the Applet class The we can use the interface Runnable, which means the Server will also be seen as a Runnable class and has to implement the run method run(). To initiate a parallel execution of the run method a Thread object must be created. In the creation a pointer to the server object (this) should be passed as parameter. With this, the Thread object will have access to the run method. The run method implemented in the server class will be executed by performing start() on the new created Thread object. NOTAS

Example with interface Runnable See and execute the program NoSincron.java Note that the Threads created in this way will have access to all resources of the server. De la otra forma es más bien una opción, (pasar un puntero al servidor cuando se crea un objeto thread) De cualquier forma, será frecuente el compartir recursos y por lo tanto la admistración de ellos es importante NOTAS

Critical regions and semaphores in Java Java provides basically two methods for controlling concurrent access to resources You can declare a whole method as a critical region (ver sincron1) You can use the semaphore of any object (ver sincron2) NOTAS