start run stop Thread class My Thread extends

  • Slides: 56
Download presentation

二. 编程 start()启动线程 run()线程体,线程的实现 stop()停止线程 1. 继承Thread class My. Thread extends. Thread { public

二. 编程 start()启动线程 run()线程体,线程的实现 stop()停止线程 1. 继承Thread class My. Thread extends. Thread { public void run() { 线程体; } public static void main(String s[]) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); mt 1. start(); mt 2. start(); } } 2. 实现Runnable接口 class My. Thread implements Runnable { public void run() { 线程体; } public static void main(String s[]) {My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); Thread t 1=new Thread(mt 1); Thread t 2=new Thread(mt 2); t 1. start(); t 2. start(); } }

四. 线程的方法 Thread. current. Thread() set. Name() get. Name() set. Priority() get. Priority() Daemon:

四. 线程的方法 Thread. current. Thread() set. Name() get. Name() set. Priority() get. Priority() Daemon: set. Daemon(true) 设置为后台线程 set. Daemon(false) 设置为应用线程

五. 例子 public class My. Thread extends Thread { private int count=0; public void

五. 例子 public class My. Thread extends Thread { private int count=0; public void set. Count() { count++; } public void run() { for (int i=0; i<10; i++) { set. Count(); System. out. println(Thread. current. Thread(). get. Name()+": "+count); } } public static void main(String[] args) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); mt 1. set. Name("MT 1"); mt 2. set. Name("MT 2"); mt 1. start(); mt 2. start(); } }

五. 例子 public class My. Thread extends Thread { private static int count=0; public

五. 例子 public class My. Thread extends Thread { private static int count=0; public void set. Count() { count++; } public void run() { for (int i=0; i<10; i++) { set. Count(); System. out. println(Thread. current. Thread(). get. Name()+": "+count); } } public static void main(String[] args) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); mt 1. set. Name("MT 1"); mt 2. set. Name("MT 2"); mt 1. start(); mt 2. start(); } }

六. 线程同步 public void push(char c) { data[idx]=c; idx++; } public char pop() {

六. 线程同步 public void push(char c) { data[idx]=c; idx++; } public char pop() { idx--; return data[idx]; }

二. 常用流类 1. 字节流 File. Input. Stream / File. Output. Stream Buffered. Input. Stream

二. 常用流类 1. 字节流 File. Input. Stream / File. Output. Stream Buffered. Input. Stream / Buffered. Output. Stream Data. Input. Stream / Data. Output. Stream Piped. Input. Stream / Piped. Output. Stream 例: File. Input. Stream fis=new File. Input. Stream(“aa. doc”); Buffered. Input. Stream bis=new Buffered. Input. Stream(fis); Data. Input. Stream dis=new Data. Input. Stream(bis);

2. 字符流-16位版本的数据流 Input. Stream. Reader / Output. Stream. Writer 用于字节流 字符流的转换 Buffered. Reader /

2. 字符流-16位版本的数据流 Input. Stream. Reader / Output. Stream. Writer 用于字节流 字符流的转换 Buffered. Reader / Buffered. Writer

3. 例: 从标准输入中读取字符串数据 import java. io. *; public class Input { public static void

3. 例: 从标准输入中读取字符串数据 import java. io. *; public class Input { public static void main(String[] args) { String s; Input. Stream. Reader ir; Buffered. Reader br; ir=new Input. Stream. Reader(System. in); br=new Buffered. Reader(ir); try { while((s=br. read. Line())!=null) { System. out. println("Read: "+s); } }catch(IOException e) { System. out. println("Read falled"); } } }

三. URL输入流 提供了使用统一资源定位符URL在网络上访问对象的方法. 隐含使用URL对象: image=get. Image(get. Document. Base(), ”t 1. gif”); 直接使用URL对象: URL image.

三. URL输入流 提供了使用统一资源定位符URL在网络上访问对象的方法. 隐含使用URL对象: image=get. Image(get. Document. Base(), ”t 1. gif”); 直接使用URL对象: URL image. Source=new URL(“http: //……”); image=get. Image(image. Source, ”t 1. gif”);

二. 编程 start()启动线程 run()线程体,线程的实现 1. 继承Thread class My. Thread extends. Thread { public void

二. 编程 start()启动线程 run()线程体,线程的实现 1. 继承Thread class My. Thread extends. Thread { public void run() { 线程体; } public static void main(String s[]) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); Mt 1. start(); Mt 2. start(); } } 2. 实现Runnable接口 class My. Thread implements Runnable { public void run() { 线程体; } public static void main(String s[]) {My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); Thread t 1=new Thread(mt 1); Thread t 2=new Thread(mt 2); t 1. start(); t 2. start(); } }

1. New Thread状态 Thread my. Thread=new Thread. Class() 2. Runnable状态 Thread my. Thread=new Threadclass();

1. New Thread状态 Thread my. Thread=new Thread. Class() 2. Runnable状态 Thread my. Thread=new Threadclass(); My. Thread. start();

3. Not Runnable状态 n n n sleep() wait() I/O动作时

3. Not Runnable状态 n n n sleep() wait() I/O动作时

六. 线程的方法 current. Thread() set. Name() get. Name() set. Priority() get. Priority() is. Alive()

六. 线程的方法 current. Thread() set. Name() get. Name() set. Priority() get. Priority() is. Alive() yield() set. Daemon(true) is. Daemon()

七. 例子 public class My. Thread extends Thread { private int count=0; public void

七. 例子 public class My. Thread extends Thread { private int count=0; public void set. Count() { count++; } public void run() { for (int i=0; i<10; i++) { set. Count(); System. out. println(Thread. current. Thread(). get. Name()+": "+count); } } public static void main(String[] args) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); mt 1. set. Name("MT 1"); mt 2. set. Name("MT 2"); mt 1. start(); mt 2. start(); } }

五. 例子 public class My. Thread extends Thread { private static int count=0; public

五. 例子 public class My. Thread extends Thread { private static int count=0; public void set. Count() { count++; } public void run() { for (int i=0; i<10; i++) { set. Count(); System. out. println(Thread. current. Thread(). get. Name()+": "+count); } } public static void main(String[] args) { My. Thread mt 1=new My. Thread(); My. Thread mt 2=new My. Thread(); mt 1. set. Name("MT 1"); mt 2. set. Name("MT 2"); mt 1. start(); mt 2. start(); } }

八. 线程同步 问题的引出: public void push(char c) { date[idx]=c; idx++; } public pop() {

八. 线程同步 问题的引出: public void push(char c) { date[idx]=c; idx++; } public pop() { idx--; return date[idx]; }

解决方法: synchronized关键字

解决方法: synchronized关键字

标准输出(System. out) n n System. out. print(“Hello World”); //print System. out. println(“Hello World”); //print

标准输出(System. out) n n System. out. print(“Hello World”); //print System. out. println(“Hello World”); //print line

标准输入(System. in) public class P { public static void main(String[] args) { int key.

标准输入(System. in) public class P { public static void main(String[] args) { int key. Input; int i=0; try{ while((key. Input=System. in. read())!=-1){ i++; System. out. println(i+"="+key. Input); } } catch(IOException e){} } }

标准错误(System. err) n 把错误信息显示给用户 System. err. println(“Warning!”);

标准错误(System. err) n 把错误信息显示给用户 System. err. println(“Warning!”);

2. 常用的I/O流 n File. Input. Stream/File. Output. Stream n Pipe. Input. Stream/Pipe. Output. Stream

2. 常用的I/O流 n File. Input. Stream/File. Output. Stream n Pipe. Input. Stream/Pipe. Output. Stream n Byte. Array. Input. Stream/ Byte. Array. Output. Stream n Sequence. Input. Stream n String. Buffer. Input. Stream

3. 过滤流 Filter. Input. Stream/Filter. Output. Stream

3. 过滤流 Filter. Input. Stream/Filter. Output. Stream

三. URL输入流 提供了使用统一资源定位符URL在网络上访 问对象的方法. 隐含使用URL对象: image=get. Image(get. Document. Base(), ”t 1. gif ”); 直接使用URL对象:

三. URL输入流 提供了使用统一资源定位符URL在网络上访 问对象的方法. 隐含使用URL对象: image=get. Image(get. Document. Base(), ”t 1. gif ”); 直接使用URL对象: URL image. Source=new URL(“http: //……”); image=get. Image(image. Source, ”t 1. gif”);

1. 定义一个日期类Date class Date { private int year=1990, month=1, day=1; public Date(){} public Date(int

1. 定义一个日期类Date class Date { private int year=1990, month=1, day=1; public Date(){} public Date(int year, int month, int day) { this. year=year; this. month=month; this. day=day; } public Date(int year) { this(year, 1, 1); } public Date(Date d) { this (d. year, d. month, d. day); } public String get. Date() { return year+"/"+month+"/"+day; } public void set. Date(int year, int month, int day) { this. year=year; this. month=month; this. day=day; } public void set. Date(int month, int day) { set. Date(this. year, month, day); } } class Ex 3 { public static void main(String[] args) { Date d 1=new Date(); System. out. println(d 1. get. Date()); Date d 2=new Date(2000, 11, 17); System. out. println(d 2. get. Date()); Date d 3=new Date(2010); System. out. println(d 3. get. Date()); Date d 4=new Date(d 2); System. out. println(d 4. get. Date()); Date d 5=d 2; System. out. println(d 5. get. Date()); } }

1. class Point{ int x, y; public static void main(String args[]){ switch(args. length){ case

1. class Point{ int x, y; public static void main(String args[]){ switch(args. length){ case 0: Point p=new Point(); p. display(); break; case 1: Point p 1=new Point(Integer. parse. Int(args[0])); p 1. display(); break; case 2: Point p 2=new Point(Integer. parse. Int(args[0]), Integer. parse. Int(args[1])); p 2. display(); break; default: System. out. println(“the arguments too long”); } //switch

public Point(int x, int y){ this. x=x; this. y=y; } public Point(int x){ this(0,

public Point(int x, int y){ this. x=x; this. y=y; } public Point(int x){ this(0, x); } public Point(){ this(0); } public void display(){ System. out. println("("+x+", "+y+")"); } } //class

2. class Multiply { public static void main(String args[]) { Multiply m=new Multiply(); m.

2. class Multiply { public static void main(String args[]) { Multiply m=new Multiply(); m. ply(Integer. parse. Int(args[0])); } public void ply (int i) { int s=1; for (int j=i; j>0; j--) {s*=j; } System. out. println(s); } }

六. 例程(对继承) class Ex 4 class Person { public static void main(String s[]) {

六. 例程(对继承) class Ex 4 class Person { public static void main(String s[]) { private String name; { Person p 1=new Person("Zhao", true); private boolean sex; p 1. get. Detail(); public Person(String name, boolean sex) Teacher t 1=new Teacher("Qian", false, 1); { this. name=name; t 1. get. Detail(); this. sex=sex; } Person p[]=new Person[5]; public void get. Detail() p[0]=new Teacher("Sun", true, 2); { System. out. println(name+sex); } p[1]=new Student("Li", true, 2); } p[2]=new Teacher("Zhou", true, 2); class Teacher extends Person p[3]=new Student("Wu", true, 2); { private int department; p[4]=new Teacher("Zheng", true, 2); public Teacher(String name, boolean sex, int depart) for (int i=0; i<p. length; i++) { super(name, sex); { if(p[i] instanceof Teacher) department=depart; } { System. out. println("Teacher"); public void get. Detail() p[i]. get. Detail(); } { super. get. Detail(); else if (p[i] instanceof Student) System. out. println(department); { System. out. println("student"); } Student s 1=(Student)p[i]; } s 1. get. Detail(); } class Student extends Person } { 同Teacher department-->classes } depart-->cls } }

import java. awt. *; …… public class Calculator pan. add(new Button("/")); { Frame f;

import java. awt. *; …… public class Calculator pan. add(new Button("/")); { Frame f; f. set. Size(200, 200); Panel pan; f. set. Visible(true); } public Calculator () public static void main(String s[]) { f=new Frame("Calculator"); { pan=new Panel(); } c. go(); public void go() { f. add("North", new Text. Field("")); f. add("Center", pan); pan. set. Layout(new Grid. Layout(4, 4)); pan. add(new Button("1")); pan. add(new Button("2")); pan. add(new Button("+")); Calculator c=new Calculator (); } }