Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Javadoc Javadoc 2 • • • • • • • • • • Special comments that can be used to create documentation for the classes we write • /** denotes the start of a javadoc comment • Is written above a class, attribute, constructor or method declaration • The first line should be a general description of the method etc. • The general description ends starting at the first line containing a @ character. @author @version @param @return @exception @see @since @serial @deprecated (only (only (only (only (also classes and interface) classes and interface) methods and constructors) methods) @throws since Javadoc 1.2) (or @serialField or @serialData) • The Java API description is built using javadoc • For more info see: http://java.sun.com/j2se/javadoc/ 68 69 Jar-files Applets • Are used to package java files to make them easier to distribute. • Eclipse has the ability to export a project to a jar file • The jar file can be made executable by specifying witch class contains the main method • Applets – Are loaded over a network connection – Runs in a browser window (or appletviewer) • The browser (Netscape, Explorer etc) – downloads the applet from the server when it finds the <APPLET>-tag – Supplies a graphical environment – Creates a object from the Applet class. – Does not permit certain operations (for security reasons) 70 An Applet is a Component 71 The lifecycle of an Applet • Is created by the browser (Constructor) • Is Initialized (init) – only once (in theory…)! • Is started (start) – each time the page is shown • Is stopped (stop) – when the user leaves the page • Final cleanup (destroy) – when the page is removed from memory 72 73 1 The anatomy of an applet init() • public void init() • public void start() • public void stop() • public void destroy(); • We usually don’t supply a constructor since the applet is not fully functioning at that time • Should contain the initialization of the applet • Usually contains the code that would otherwise be in the constructor. • Is called as soon as the browser has done its initialization of the Applet (setting up graphics environment and so on) 74 75 start() stop() • Om start omdefinierats så är det bäst att ta med stop också • Anropas när användaren bläddrar bort från sidan • Stoppar beräkningar trådar animeringar (ingen kan se sidan ändå) • Is called each time the page is shown Performs the actual work (or more likely starts up a separate Thread) 76 77 A small Applet Doodle import java.awt.*; import java.applet.*; • Doodle.java public class SmallApplet extends Applet { Label field; public void init() { field= new Label("Hej !"); setLayout(new FlowLayout()); add(field); validate(); } } 78 79 2 What you can’t do in an Applet Starting an applet from a webpage <APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGTH=anInt> </APPLET> <APPLET CODE=“AppletSubclass.class” ARCHIVE=“file1, file2” WIDTH=anInt HEIGTH=anInt> </APPLET> • Parameters: <APPLET CODE=AppletButton.class CODEBASE=example WIDTH=350 HEIGTH=60> <PARAM NAME=titel VALUE=“Lets try!”> </APPLET> String title=getParameter(“titel”); • An Applet can not (without some extra work) – call ‘native methods’ – read and write files on the client computer. – setup network connections to other computers except the server from where it originated – Start programs on the client – read certain system properties – Windows opened by applets usually have a different look then other windows. 80 Threads 81 Threads in a program • A Thread – Is a process in a program – Shares resources with other threads in the same application. – Can execute at the same time as another thread – Can be created in two ways • The ’Garbage collector’ is executed in a separate thread Two Threads 82 Two ways of creating a new thread 83 Extend Thread • Extend the class Thread and override the method run • Implement the Runnable interface 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{ sleep((long) (Math.random()*1000)); } catch (InterruptedException e) {} } System.out.println(”Done !” +getName()); } } 84 85 3 Testing Implement Runnable public class Clock extends Applet implements Runnable { private Thread clockThread =null; public class TwoThreadsTest { public static void main(String[] args) { new SimpleThread(”Jamaica”).start(); new SimpleThread(”Fiji”).start(); } } 0 0 1 2 3 public void start() { if (clockThread==null) { clockThread=new Thread(this,"Clock"); clockThread.start(); } } Jamaica Fiji Jamaica Jamaica Jamaica public void run() { … 86 Synchronizing threads • Every object in Java has a built-in lock, which means that it has at its disposal a boolean locked variable and a queue for blocked threads. • Mutual exclusion code blocks are created using the synchronized keyword as follows: Object x = new Object(); synchronized(x) { // mutual exclusive access for all threads which share x } • A thread can only run in the synchronized(x) region if it has acquired x's lock, otherwise if it tries to run, it is blocked. • When a thread leaves the region it gives up x's lock and wakes up a single blocked thread (if any). 88 87 Synchronizing threads • The other way to create mutual exclusion regions using the synchronized keyword is as a qualifier for member functions like this: class MyClass { public synchronized void foo() { // ... } } • In this case, if multiple threads share the object MyClass c = new MyClass() then all calls to c.foo() are mutually exclusive. 89 4