|
Posted on 2007-10-09 00:25 Java蜘蛛人 --郑成桥 阅读(380) 评论(0) 编辑 收藏
简单的概括下多线程的用法
多线程
class NewThread implements Runnable
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
Thread t;
![](/Images/OutliningIndicators/InBlock.gif)
NewThread()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
t=new Thread(this);
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("NewTread 启动了啊");
![](/Images/OutliningIndicators/InBlock.gif)
t.setPriority(3);
![](/Images/OutliningIndicators/InBlock.gif)
t.start();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
public void run()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
for(int i=5;i>0;i--)
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("NewThread:"+i);
![](/Images/OutliningIndicators/InBlock.gif)
Thread.sleep(500);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
catch(Exception e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
class Test2 extends Thread
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
Thread t;
![](/Images/OutliningIndicators/InBlock.gif)
Test2()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
t=new Thread(this);
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("Test启动了");
![](/Images/OutliningIndicators/InBlock.gif)
t.setPriority(4);
![](/Images/OutliningIndicators/InBlock.gif)
t.start();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
public void run()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
for(int i=5;i>0;i--)
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("Test2::"+i);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
catch(Exception e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
public class ThreadDemo implements Runnable
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
Thread a;
![](/Images/OutliningIndicators/InBlock.gif)
ThreadDemo()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
a=new Thread(this);
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("执行到main外");
![](/Images/OutliningIndicators/InBlock.gif)
a.setPriority(5);
![](/Images/OutliningIndicators/InBlock.gif)
a.start();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
static int aa=0;
![](/Images/OutliningIndicators/InBlock.gif)
public void run()
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
try
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("main::开始了啊");
![](/Images/OutliningIndicators/InBlock.gif)
for(int i=5;i>0;i--)
![](/Images/OutliningIndicators/InBlock.gif)
System.out.println ("main:"+i);
![](/Images/OutliningIndicators/InBlock.gif)
if(aa++==2)
![](/Images/OutliningIndicators/InBlock.gif)
a.yield();a.sleep(1000);
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
catch(Exception e)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
public static void main (String[] args)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](/Images/OutliningIndicators/InBlock.gif)
new NewThread();
![](/Images/OutliningIndicators/InBlock.gif)
new Test2();
![](/Images/OutliningIndicators/InBlock.gif)
new ThreadDemo();
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
对象流的存储。。。
import java.util.*;
import java.io.*;
public class ObjectFileTest
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
public static void main (String[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Manager boss=new Manager("xiaoqiao",80000,2008,05,06);
boss.setBoss(5000);
System.out.println (boss);
Employee [] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee("xiongdi",8,2007,05,06);
staff[2]=new Employee("asd",1000,2004,05,06);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("zcq.doc"));
out.writeObject(staff);
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("zcq.doc"));
Employee[] newStaff=(Employee[])in.readObject();
in.close();
for(Employee e:newStaff)
System.out.println (e);
}
catch(Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
e.printStackTrace();
}
}
}
class Employee implements Serializable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
private String name;
private double salary;
private Date hireDay;
public Employee()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
}
public Employee(String n,double s,int year,int month,int day)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public String getName()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return name;
}
public double getSalary()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return salary;
}
public Date getHireDay()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return hireDay;
}
public String toString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return "name:"+name
+" salary:"+salary
+" 时间是:"+hireDay;
}
}
class Manager extends Employee
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
private double bouss;
public Manager(String n,double s,int year,int month,int day)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
super(n,s,year,month,day);
}
public double getSalary()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
double aa=super.getSalary();
return aa+bouss;
}
public void setBoss(double b)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
bouss=b;
}
public String toString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return super.toString()+"boss:"+bouss;
}
}
![](/Images/OutliningIndicators/None.gif)
保存对象引用问题
一个经理共享一个秘书和一个雇员
。
import java.util.*;
import java.io.*;
public class ObjectRefTest
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
public static void main (String[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Employee harry=new Employee("harry asd",5000,1989,10,1);
Manager boss=new Manager("Carl Cracker",8000,1987,12,12);
boss.setSecretary(harry);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=harry;
staff[2]=new Employee("zcq123",8000,1990,3,15);
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("zcq.doc"));
out.writeObject(staff);
out.close();
ObjectInputStream in=new ObjectInputStream(new FileInputStream("zcq.doc"));
Employee[] newStaff=(Employee[])in.readObject();
in.close();
newStaff[1].ticheng(10);
for(Employee e:newStaff)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
System.out.println (e);
}
}
catch(Exception e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
e.printStackTrace();
}
}
}
class Employee implements Serializable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
private String name;
private double salary;
private Date hireDay;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) public Employee() {}
public Employee(String n,double s,int year,int month,int day)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
name=n;
salary=s;
GregorianCalendar calendar= new GregorianCalendar(year,month-1,day);
hireDay=calendar.getTime();
}
public String getName()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return name;
}
public double getSalary()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return salary;
}
public Date getHireDay()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return hireDay;
}
public void ticheng(double aa)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
double bb=salary*aa/100;
salary=salary+bb;
}
public String toString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return getClass().getName()+" name:"
+name+" salary:"+salary
+" hireday"+hireDay;
}
}
class Manager extends Employee
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
public Manager(String n,double s ,int year,int month,int day)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
super(n,s,year,month,day);
secretary=null;
}
public void setSecretary(Employee s)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
secretary=s;
}
public String toString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return super.toString()+" secretary"+secretary;
}
public Employee secretary;
}
![](/Images/OutliningIndicators/None.gif)
适合初学者看的克隆 一看就懂
public class Test
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
public static void main (String[] args)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Test1 aa=new Test1();
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Test1 bb=aa.clone();
}
catch(CloneNotSupportedException e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
e.printStackTrace();
}
}
}
class Test1 implements Cloneable
![](/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](/Images/OutliningIndicators/ContractedBlock.gif) {
public Test1 clone() throws CloneNotSupportedException //抛出这个异常 ..
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
Test1 aa=(Test1)super.clone();
return aa;
}
}
![](/Images/OutliningIndicators/None.gif)
|