Java学习笔记(五)

本文介绍了Java中的内部类和匿名内部类的概念及用法,并详细讲解了两种创建线程的方法,包括通过继承Thread类和实现Runnable接口的方式。此外,还探讨了线程同步的重要性及其实现方法。

35_内部类和匿名类

1.内部类:

内部类可以随意使用外部类的对象,但它们不是继承关系

class A {
	int i;

	class B {
		int j;

		int funB() {
			/* return A.this.i + this.j; */
			return i + j;
		}
	}
}
public class Test {
	public static void main(String[] args) {
		A a = new A();
		/*A.B b = new A().new B();*/
		A.B b = a.new B();
		
		a.i = 1;b.j=99;
		System.out.println(b.funB());
	}
}

2.匿名内部类:

interface A{
	public void dosomething();
}
class B {
	public void funB(A a) {
		System.out.println("B的函数");
		a.dosomething();
	}
}
/*class ImpA implements A {
	public void dosomething() {
		System.out.println("do");
	}
}*/
public class Test {
	public static void main(String[] args) {
		/*ImpA impa = new ImpA();
		A a = impa;  //向上转型
		
		B b = new B();
		b.funB(a);*/
		B b = new B();
		b.funB(new A() {
			public void dosomething() {
				System.out.println("内部匿名类");
			}
		});
	}
}



36、37、38_线程

1.进程与线程的区别:

多进程:
在操作系统中能(同时)运行多个任务(程序)。
多线程:
在同一应用程序中有多个顺序流(同时)执行。


2.实现线程的两个方法:
方式一:
定义一个线程类,它继承类Thread并重写其中的方法run(),方法run()称为线程体;
由于java只支持单继承,该方法的类不能再继承其他类。

class FirstThread extends Thread {
	public void run() {
		for (int i = 0; i < 20; i++) {
			System.out.println("FirTh-->" + i);
		}
	}
}
public class Test {
	public static void main(String[] args) {
		FirstThread ft = new FirstThread();
		//启动线程,这样使用错误:ft.run()
		ft.start();
		
		for(int i = 0;i < 50;i++){
			System.out.println("main-->" + i);
		}
	}
}

2.方式二(常用):
提供一个实现接口Runnable的类作为线程的目标对象,在初始化一个Thread类或Thread子类的线程对象时,把目标对象传递给这个线程实例,由该实例对象提供线程体。

线程的优先级最大10,最小。

class RunnableImp implements Runnable {
	public void run() {
		for (int i = 0; i < 20; i++) {
			System.out.println("Runnable-->" + i);
			if (i == 15) {
				try {
					Thread.sleep(3000);
					// 让当前线程让出CPU,然后开始新一轮的抢夺CPU(有可能当前线程又再次抢到CPU)
					Thread.yield();
				} catch (Exception e) {
					System.out.println(e);
				}
			}
		}
	}
}
public class Test {
	public static void main(String[] args) {
		RunnableImp rm = new RunnableImp();
		Thread t = new Thread(rm);
		t.start();
	}
}




39_同步语法
1.同步线程:

当不同线程使用相同的数据的时候可能会出现错误。

class SynThread implements Runnable {
	int i = 100;

	public void run() {
		//同步锁
		synchronized (this) {
			while (true) {
				System.out.println(Thread.currentThread().getName() + i);
				i--;
				if (i < 0)
					break;
			}
		}
	}
}
public class Test {
	public static void main(String[] args) {
		SynThread synth = new SynThread();
		Thread t1 = new Thread(synth);
		Thread t2 = new Thread(synth);

		t1.setName("A ");
		t2.setName("B ");

		t1.start();
		t2.start();
	}
}

2.同步锁到底锁的是什么?

一个线程获得了一个对象的同步锁,那么这个对象所有的同步代码都不能被其它线程执行。
同步方法(函数)和同步代码块功能相同。

class Service {
	public void fun1() {
		synchronized (this) {
			try {
				Thread.sleep(3000);
			} catch (Exception e) {
				System.out.println(e);
			}
			System.out.println("fun1");
		}
	}

	public void fun2() {
		synchronized (this) {
			System.out.println("fun2");
		}
	}
}
class MyThread1 implements Runnable {
	private Service ser;

	MyThread1(Service ser) {
		this.ser = ser;
	}

	public void run() {
		ser.fun1();
	}
}
public class Test {
	public static void main(String[] args) {
		Service ser = new Service();
		Thread t1 = new Thread(new MyThread1(ser));
		Thread t2 = new Thread(new MyThread2(ser));
		
		t1.start();
		t2.start();
	}
}


40_数组

数组是引用类型的

class Test1 {
	public static void main(String[] args) {
		// 数组的静态声明法
		int a[] = { 1, 2, 3, 4, 5, 6 };
		// 数组的动态声明法
		int b[] = new int[10];

		// 二维数组
		int c[][] = { { 1, 2, 3 }, { 4, 5, 6 }, { 3, 6 }, { 9 } };
		int d[][] = new int[5][5];
		for (int i = 0; i < c.length; i++)
			for (int j = 0; j < c[i].length; j++)
				System.out.print(c[i][j] + " ");
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值