线程是进程的一个实体,是CPU调度和分配的基本单位,其本身不拥有系统资源,只含有程序计数器,寄存器,和栈等一些运行时不可比少的基本资源,他的存在是为进程服务的,同属一个进程的线程共享进程所拥有的全部资源。
进程是具有一定独立功能的程序块关于某个数据集合上的一次运行活动,他是系统进行资源调度分配的一个独立单位
程序是一组指令的集合,由多个进程共同完成,他是一个静态的实体,没有执行的含义。
程序是一组指令集和,静态的,没有执行含义,进程是动态的实体,有生命周期,一个进程和一个程序对应,只有一个,但一个程序可以有多线程,或者一个进程都没有,进程还有并发性
TIP 理解概念,有助于掌握多线程开发。
/*
* write a class, extends Thread and override run method.
* In run method, print 1 to 10.
* In main method, create an instance of this class and start it.
*/
public class MyThread extends Thread {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + 1);
}
}
}
/*
* write a class, implements Runnable and override run method.
* In run method, print 1 to 10.
* In main method, create an instance of this class and create a Thread with it, then start the thread.
*/
class MyThread_impl implements Runnable {
public static void main(String[] args) {
MyThread_impl runnable = new MyThread_impl();
Thread thread = new Thread(runnable);
thread.start();
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + 1);
}
}
}
start()
一共10级,1-10,数字越大,级别越高,MIN_PROORITY = 1. NORMAL_PRIORITY = 5, MAX_PRIORITY = 10, by default is NORMAL_PRIORITY,
public class TestStop extends Thread {
public static void main(String[] args) {
TestStop ts = new TestStop();
ts.start();
}
@Override
public void run() {
int count = 0;
while (this.isAlive()) {
System.out.println("Running... " + count + " times" + isAlive());
count++;
if (count == 5) {
this.interrupt(); // Interrupt the thread after 5 iterations
System.out.println("Stopping the thread...");
}
}
}
}
stop 是一种粗暴的终止线程行为,没有任何清除操作,因此不安全。
而suspend会导致锁死。
应该在线程里面加入isActive标志是否活跃,如果等待过长时间,可以使用 interrupt()中断等待。
但是sleep只是睡眠特定的时间,再次可执行,而不是立即执行,因此不好使
class Person implements Serializable {
private String name;
private int age;
private String birthDate;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Person2 implements Externalizable {
private String name;
private int age;
private Date birthDate;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'writeExternal'");
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'readExternal'");
}
}
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Date;
class Person implements Serializable {
private String name;
private int age;
private String birthDate;
public Person() {
}
public Person(String name, int age, String birthDate) {
this.name = name;
this.age = age;
this.birthDate = birthDate;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Person2 implements Externalizable {
private String name;
private int age;
private Date birthDate;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'writeExternal'");
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'readExternal'");
}
}
public class IO {
public void write() throws IOException {
FileOutputStream fos = new FileOutputStream("./p.txt", true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person("Alice", 30, "1993-05-15");
oos.writeObject(p);
oos.flush();
}
public void read() throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("./p.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person p = (Person) ois.readObject();
System.out.println(p.getName() + " " + p.getAge() + " " + p.getBirthDate());
}
public static void main(String[] args) throws ClassNotFoundException {
IO e = new IO( );
try {
// e.write();
e.read();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
如果涉及到敏感信息的话,例如密码,通常需要考虑到密码泄漏问题,一般调用readObject()和writeObject()的方法实现, 而这两个方法需要重写
作为序列化的唯一ID, 初始化的时候前面加上final,是用来做id比对的,如果id同样的,默认可以进行反序列化。