JAVA深入

来源:互联网 发布:js遍历 元素集合 编辑:程序博客网 时间:2024/06/05 05:16

JAVA深入第四章

本章学习要点:

  • 多线程

  • 注解

多线程

什么是多线程
如果在一个进程中同时运行了多个线程,用来完成不同的工作,则称之为“多线程”
多个线程交替占用CPU资源,而非真正的并行执行
多线程好处
充分利用CPU的资源
简化编程模型
带来良好的用户体验
Thread类
Java提供了java.lang.Thread类支持多线程编程
主线程
main()方法即为主线程入口
产生其他子线程的线程
必须最后完成执行,因为它执行各种关闭动作
在Java中创建线程的两种方式
继承java.lang.Thread类
实现java.lang.Runnable接口
(1)继承Thread类创建线程
定义MyThread类继承Thread类
重写run()方法,编写线程执行体
创建线程对象,调用start()方法启动线程
public class Test1 {
public static void main(String[] args) {
MyThread myThread =new MyThread();
MyThread myThread1 =new MyThread();
myThread.start();
myThread1.start();

}

}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(“你好来自线程” + Thread.currentThread().getName() + “-” + i);

        }    }}

(2)实现Runnable接口创建线程
定义MyRunnable类实现Runnable接口
实现run()方法,编写线程执行体
创建线程对象,调用start()方法启动线程
public class TestArrayList {

public static void main(String[] args) {
// 实例化线程
MyArrayList myRunable = new MyArrayList();
Thread thread = new Thread(myRunable);
Thread thread1 = new Thread(myRunable);
thread.start();
thread1.start();
}

}

class MyArrayList implements Runnable {
private List list = new ArrayList();

@Override
public void run() {
// System.out.println(Thread.currentThread().getName() + “正在运行…”);
for (int i = 0; i < 100; i++) {
list.add(i + “”);
try { Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(i + “的位置的值=>” + list.get(i));
}
}

}

线程的方法
Thread.currentThread().getName() 主线程名
thread1.currentThread().getName() 子线程名
Thread.currentThread().getPriority() 主线程的优先级
thread1.getPriority() 子线程优先级
Thread.currentThread().setPriority(Thread.MAX_PRIORITY); 修改主线程优先级
thread1.setPriority(Thread.MIN_PRIORITY);修改子线程优先级
优先级默认是5
Thread.sleep(time);睡眠多久
thread.join();等待次加入的进程结束

多线程共享数据引发的问题
同步方法
使用synchronized修饰的方法控制对类成员变量的访问
访问修饰符 synchronized 返回类型 方法名(参数列表){……}
或者
synchronized 访问修饰符 返回类型 方法名(参数列表){……}
public class TestRunable8 {

public static void main(String[] args) {
// 实例化线程
MyRunable8 myRunable = new MyRunable8();
Thread thread1 = new Thread(myRunable, “张杰”);
Thread thread2 = new Thread(myRunable, “黄牛”);
Thread thread3 = new Thread(myRunable, “钱代理”);
thread1.start();
thread2.start();
thread3.start();

}

}

class MyRunable8 implements Runnable {

private int count = 10; // 当前票数
private int num = 0; // 购买的票数
@Override
public void run() {
while (true) {
// 调用同步方法
if (!sale()) {
break;
}

    }}

// 同步方法==>锁
synchronized private boolean sale() {
// 结束
if (count == 0)
return false;
num++;
count–;
try {
Thread.sleep(100); //
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + “抢到第” + num + “张票,剩余” + count + “张票!”);
return true;
}

}

同步块
synchronized(syncObject){
//需要同步的代码
}
public class TestRunable9 {

public static void main(String[] args) {
// 实例化线程
MyRunable9 myRunable = new MyRunable9();
Thread thread1 = new Thread(myRunable, “张杰”);
Thread thread2 = new Thread(myRunable, “黄牛”);
Thread thread3 = new Thread(myRunable, “钱代理”);
thread1.start();
thread2.start();
thread3.start();

}

}

class MyRunable9 implements Runnable {

private int count = 10; // 当前票数
private int num = 0; // 购买的票数

@Override
public void run() {

while (true) {
//同步块
synchronized (this) {
// 结束
if (count == 0)
break;
num++;
count–;

try {
Thread.sleep(100); //
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + “抢到第” + num + “张票,剩余” + count + “张票!”);

        }    }}

}

注解

元数据:描述数据的数据
元数据的用处:文档编制、编译器检查、代码分析等
Java注解:
Java代码里的特殊标记。它为在代码中添加用Java程序无法表达的额外信息提供了一种形式化的方法。
注释:注释不会被程序所读取
注解:注解可以在编译、类加载、运行时被读取

注解的分类
内建注解:标准注解类型
限定重写父类方法:@Override
标示已过时:@Deprecated
抑制编译器警告:@SuppressWarnings
元注解:修饰其他的注解定义,如: @Target 、@Retention
自定义注解:注解类型是一种接口
使用关键字@interface定义
四个元注解类型
@Target
指定被其修饰的注解能用于修饰哪些程序元素
成员变量value为ElementType 枚举类型
@Retention
指定该注解可使用反射读取
成员变量value:RetentionPolicy 枚举类型
@Documented
指定被其修饰的注解将被JavaDoc工具提取成文档
@Inherited
指定被其修饰的注解将具有继承性
自定义注解
使用@interface
关键字声明
注解也可以带成员变量
@Target(METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataBaseType3{
String typeConn() default “SQL”;
}