Java学习笔记 线程,注解(注释)...

本文详细介绍了Java中自定义注解的使用,并通过实例展示了如何利用注解实现对象代理,以及在多线程环境下利用同步机制确保数据一致性。重点探讨了线程同步问题及解决策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笔记

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();
    }
}

总结

来日再见当初的浅薄,学习需要认真和积累

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值