Threads Page is loading Load new page Browser
Threads Page is loading Load new page Browser still responds to user (can read pages in other tabs)
Threads q Advantages Ø some tasks inherently parallel – can exploit the underlying architecture Ø responsive applications – user can still use application while operation pending Ø sharing of resources (communication) is easier Ø switching between threads is faster than process switching Ø overhead in creating threads is small
Threads Single-threaded process Global Data Files, I/O Registers thread Stack Multi-threaded process Global Data Files, I/O Registers Stack
Java thread States new start() runnable wait for I/O (file, input) exits run() method I/O available blocked dead
Creating Threads q Extend the Thread class, override run() method class Fetch. Thread extends Thread { private String url; public Fetch. Thread(String url) { this. url = url; } public void run() { // 1. connect to server // 2. download page // 3. update view widget } } // LATER ON. . . Thread fetch = new Fetch. Thread(“www. google. com”); fetch. start();
Creating Threads q Implement Runnable interface (only one method run()) class Fetch. Thread implements Runnable { private String url; public Fetch. Thread(String url) { this. url = url; } public void run() { // 1. connect to server // 2. download page // 3. update view widget } } // LATER ON. . . Runnable target = new Fetch. Thread(“www. google. com”); Thread fetch = new Thread(target); fetch. start();
MIDlets and Threads (I) class Demo. MIDlet implements Command. Listener, Runnable {. . . . public void run() { if (last. Cmd == add. Cmd) { // handle add operation } else if (last. Cmd == show. Cmd) { // handle show operation } }. . . . public void command. Action(Command c, Displayable d) { Form ack. Form = new Form(“Working. . . ”); display. set. Current(ack. Form); Thread t = new Thread(this); t. start(); }
MIDlets and Threads (II) class Demo. MIDlet implements Command. Listener, Runnable {. . . . class Update. Thread implements Runnable { Command last. Cmd; public void Update. Thread(Command c) { last. Cmd = c; } public void run() { if (last. Cmd == add. Cmd) { // handle add operation } } }. . . . public void command. Action(Command c, Displayable d) { Form ack. Form = new Form(“Working. . . ”); display. set. Current(ack. Form); Thread t = new Thread(new Update. Thread(c)); t. start(); }
Demo
Taking Pictures q Setting up the Control player = Manager. create. Player("capture: //video"); player. realize(); video. Control = (Video. Control) player. get. Control("Video. Control"); Form form = new Form("Camera Form"); Item item = (Item) video. Control. init. Display. Mode( GUIControl. USE_GUI_PRIMITIVE, null); form. append(item); m. Display. set. Current(form); q Taking the picture byte[] raw = video. Control. get. Snapshot(null); Image image = Image. create. Image(raw, 0, raw. length);
Taking Pictures q Permissions javax. microedition. media. control. Record. Control javax. microedition. media. control. Video. Control. get. Snapshot
- Slides: 11