笔记
Annotation
// 创建config.properties文件
// 文件中存放的内容person=com.linj.thread.Student
// 自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)// 注释的范围
@Target(ElementType.FIELD) // 注释的类型
public @interface StudentAnnotation {
int value(); // 注释一个int值
}
// 注解工厂
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class Factory {
public static Person creatPerson() {
Person person = null;
Properties p = new Properties();
try {
p.load(new FileInputStream("config.properties"));
String clazzName = p.getProperty("person");
Class clazz = Class.forName(clazzName);
person = (Person) clazz.newInstance();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return person;
}
}
// 在一个类中
import java.lang.reflect.Field;
public class Student extends Person{
@StudentAnnotation(18)
private int age;
private String name;
public String clazz;
public Student() {
// 得到student类的Class的对象,this指test中的Student对象zhangsan
Class clazz = this.getClass();
try {
Field field = clazz.getDeclaredField("age");
StudentAnnotation sa = field.getAnnotation(StudentAnnotation.class);
// 得到age属性的StudentAnnotation注解,如果没有返回null
if(sa != null) {
int i = sa.value();
field.setAccessible(true);
field.set(this, i);
field.setAccessible(false);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public int getAge() {
return age;
}
@Override
public void sleep() {
System.out.println("困");
}
}
多线程
// 火车票售票,线程同步,安全问题
public class TestThread implements Runnable{
int count = 120;
public synchronized void doIt() {
if(count > 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "车票剩余" + --count);
}
}
@Override
public void run() {
while (true) {
doIt();
}
}
public static void main(String[] args) {
TestThread t = new TestThread();
Thread tA = new Thread(t,"1");
Thread tB = new Thread(t,"2");
Thread tC = new Thread(t,"3");
Thread tD = new Thread(t,"4");
Thread tE = new Thread(t,"5");
Thread tF = new Thread(t,"6");
tA.start();
try {
tB.join();
tB.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// tB.start();
// tC.start();
// tD.start();
// tE.start();
// tF.start();
}
}
// 两个线程锁死的情况
public class MyRunnable1 implements Runnable{
String lock1 = "a";
String lock2 = "b";
@Override
public void run() {
synchronized (lock1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等lock2");
synchronized (lock2) {
System.out.println("我要走了");
}
}
}
public class MyRunnable2 implements Runnable{
String lock1 = "a";
String lock2 = "b";
@Override
public void run() {
synchronized (lock2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("等lock1");
synchronized (lock1) {
System.out.println("我要走了");
}
}
}
}
// 测试类部分代码
MyRunnable1 r1= new MyRunnable1();
MyRunnable2 r2= new MyRunnable2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
// 银行取款
public class BankTest implements Runnable{
int money = 1000;
public synchronized void getMoney() {
if(money >= 100) {
try {
Thread.sleep(50);
System.out.println(Thread.currentThread().getName() +
"取款100 \n余额:" + (money -= 100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
while(true) {
getMoney();
}
}
public static void main(String[] args) {
BankTest t = new BankTest();
Thread a = new Thread(t,"a");
Thread b = new Thread(t,"b");
a.start();
b.start();
}
}
总结
来日再见当初的浅薄,学习需要认真和积累