java 多线程学习笔记

java

1.多线程实现方式一:自定义一个类,继承Thread类,覆盖Thread类中继承来的run()方法。

//自定义类,继承Thread类

public class MyThread extends Thread {

//重写类里对run()方法

@Override

public void run() {

for (int i = 0; i < 10; i++) {

try {

Thread.sleep(100);

System.out.println(getName() + ":" + i);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

    public static void main(String[] args) {

//创建一个线程对象

MyThread myThread = new MyThread();

MyThread myThread1 = new MyThread();

myThread.start();//启动线程

myThread1.start();

//匿名内部类方式实现

new Thread() {

@Override

public void run() {

//// TODO

}

}.start();

}

2.多线程实现方式二:自定义一个类,实现Runnable接口,实现其中的的run()方法。

方式二好处:可以避免由于java单继承带来的局限性。适合多个相同程序的代码去处理同一个资源的情况,把线程同程序的代码,数据有效分离,较好的体现了面向对象的设计思想。

//自定义类,实现Runnable接口

public class MyRunnable implements Runnable {

@Override

public void run() {

for (int i = 0; i < 10; i++) {

try {

Thread.sleep(100);

System.out.println(Thread.currentThread().getName() + ":" + i);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

    public static void main(String[] args) {

MyRunnable my = new MyRunnable();

Thread t1 = new Thread(my);

Thread t2 = new Thread(my);

t1.start();//启动线程

t2.start();

//匿名内部类方式实现

new Thread(new Runnable() {

@Override

public void run() {

//// TODO

}

}).start();

}

3.多线程实现方式三:通过实现Callable接口,创建线程池的方式。

//Callable:是带泛型的接口,这里指定的泛型其实是call()方法的返回值类型

public class MyCallable implements Callable<Integer> {

private int number;

public MyCallable(int number) {

this.number = number;

}

@Override

public Integer call() throws Exception {

int sum = 0;

for (int i = 0; i <= number; i++) {

sum += i;

}

return sum;

}

}

public static void main(String[] args) throws ExecutionException, InterruptedException {

//创建线程池对象,创建两个线程对象。

ExecutorService pool = Executors.newFixedThreadPool(2);

//可以执行Callable对象或者Runnable对象代表的线程

Future<Integer> future = pool.submit(new MyCallable(100));//带返回结果

pool.submit(new MyRunnable());

//匿名内部类

pool.execute(new Runnable() {

@Override

public void run() {

//// TODO

}

});

Integer i = future.get();

System.out.println(i);

//结束

pool.shutdown();

}

 4.多线程的同步synchronized(同步代码块,同步方法)

public class MyRunnable implements Runnable {

private int money = 100;

@Override

public void run() {

doFunction();

}

private void doFunction() {

while (true)

//同步代码块

synchronized (MyRunnable.clss) {

if (money > 0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + ":" + money--);

}

}

}

}

public class MyRunnable implements Runnable {

private int money = 100;

@Override

public void run() {

doFunction();

}

//同步方法,这里是锁对象是this;如果方法是静态方法,锁对象是该类的字节码文件对象

private synchronized void doFunction() {

while (true)

if (money > 0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + ":" + money--);

}

}

}

5.多线程同步Lock(JDK5以后才有的)

public class MyRunnable implements Runnable {

private int money = 100;

//定义锁对象

private Lock lock = new ReentrantLock();

@Override

public void run() {

doFunction();

}

private void doFunction() {

while (true) {

try {

//加锁

lock.lock();

if (money > 0) {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + ":" + money--);

}

} finally {

//释放锁

lock.unlock();

}

}

}

}

以上是 java 多线程学习笔记 的全部内容, 来源链接: utcz.com/z/392725.html

回到顶部