前言
自学java的学习笔记博客,仅供本人学习研究用
多线程
对于线程和进程的概念一直有大体的概念,使用过c++11 , qt的多线程,但是仍然感觉对于线程不够深入了解。 下面这个图看起来比较直观的讲明多线程和单线程的区别,但是有一点需要注意,同一个cpu的同一个核心,在任意时刻只能运行一条指令。 意思就是,假如有一台电脑是单cpu ,单核心。那么在这台电脑上运行多线程的程序是没有意义的,而且效率有可能会更加慢,原因在于切换线程会消耗时间和资源。 cpu执行分为io操作和计算操作,如果有一个合适的调度在io和计算操作两者之间进行调度,那么单核多线程可能还是会效率更高的。这一块可以参阅操作系统的心智哲学这本书中,关于进程调度方面的介绍。 下面这个链接介绍了cpu 进程 多线程。 单核处理器、多核处理器、多处理器与多线程编程,cpu调度
特意把西部开源的log截图下来,感谢西部开源的讲师秦疆老师,免费将这些学习资源开放出来。
java创建线程
继承thread类方式
public class TestThread1 extends Thread{
@Override
public void run() {
for (int i=0;i<1000;++i)
System.out.println("子线程 "+i);
}
public static void main(String[] args) {
TestThread1 test = new TestThread1();
test.start();
for (int i=1000;i<2000;++i)
System.out.println("主线程 "+i);
}
}
实现Runnable接口
public class TestThread3 implements Runnable{
@Override
public void run() {
for (int i=0;i<10;++i)
System.out.println("子线程 "+i);
}
public static void main(String[] args) {
TestThread3 test = new TestThread3();
new Thread(test,"a").start();
new Thread(test,"b").start();
new Thread(test,"c").start();
for (int i=10;i<20;++i)
System.out.println("主线程 "+i);
}
}
在这里可以引申一下,看一下Runnable源码
package java.lang;
@FunctionalInterface
public interface Runnable {
void run();
}
再看一下Thread类的实现
可以看到这里判断target是否为空,不为空则调用target的run方法
那么这个target是什么呢?仔细看下面图片 发现这个target就是Runnable类型
再次查询发现调用关系如下
第一种实际上Thread也是继承了Runnable接口并实现 第二种使用实现Runnable接口 将其放入线程类 只是由Thread线程类在自己的run中去运行罢了
实现Callable接口
Callable 可以抛出异常 可以有返回值
import java.util.concurrent.*;
public class TestCall implements Callable<Boolean> {
public TestCall(String str){
name = str;
}
private String name;
@Override
public Boolean call() throws Exception {
for (int i = 0; i < 100; i++) {
System.out.println(name + " thread: "+i);
}
return true;
}
public static void main(String[] args) throws ExecutionException,InterruptedException {
TestCall t1 = new TestCall("t1");
TestCall t2 = new TestCall("t2");
TestCall t3 = new TestCall("t3");
ExecutorService ser = Executors.newFixedThreadPool(3);
Future<Boolean> r1 = ser.submit(t1);
Future<Boolean> r2 = ser.submit(t2);
Future<Boolean> r3 = ser.submit(t3);
System.out.println(r1.get());
System.out.println(r2.get());
System.out.println(r3.get());
ser.shutdownNow();
}
}
静态代理模式
public class StaticProxy {
public static void main(String[] args) {
WeddingCompany weddingCompany = new WeddingCompany(new You());
weddingCompany.HappyMarry();
}
}
interface Marry{
void HappyMarry();
}
class You implements Marry{
@Override
public void HappyMarry() {
System.out.println("结婚!");
}
}
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target){
this.target = target;
}
@Override
public void HappyMarry() {
begin();
this.target.HappyMarry();
end();
}
private void begin(){
System.out.println("结婚之前!");
}
private void end(){
System.out.println("结婚之后!");
}
}
lambad
类似c++11的lambda 但是也有很多不同
package com.Thread.Demo01;
public class TestLambda {
static class Like2 implements ILike{
@Override
public void lambda(int a) {
System.out.println("i like lambda2!"+a);
}
}
public static void main(String[] args) {
ILike like = new Like();
like.lambda(1);
like = new Like2();
like.lambda(2);
class Like3 implements ILike{
@Override
public void lambda(int a) {
System.out.println("i like lambda3!"+a);
}
}
like = new Like3();
like.lambda(3);
like = new ILike() {
@Override
public void lambda(int a) {
System.out.println("i like lambda4!"+a);
}
};
like.lambda(4);
like = (int a)->{
System.out.println("i like lambda5!"+a);
};
like.lambda(5);
like=(a)->{
System.out.println("i like lambda6!"+a);
};
like.lambda(6);
like=a->{
System.out.println("i like lambda7!"+a);
};
like.lambda(7);
}
}
interface ILike{
void lambda(int a);
}
class Like implements ILike{
@Override
public void lambda(int a) {
System.out.println("i like lambda!"+a);
}
}
线程停止
线程休眠
线程结合
线程状态
这里特别提醒一下 线程结束后不能再次启动
线程优先级
package www.xunlu.Thread;
public class Demo01 {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread thread1 = new Thread(myPriority,"1");
Thread thread2 = new Thread(myPriority,"2");
Thread thread3 = new Thread(myPriority,"3");
Thread thread4 = new Thread(myPriority,"4");
Thread thread5 = new Thread(myPriority,"5");
Thread thread6 = new Thread(myPriority,"6");
Thread thread7 = new Thread(myPriority,"7");
thread1.start();
thread2.setPriority(1);
thread2.start();
thread3.setPriority(4);
thread3.start();
thread4.setPriority(Thread.MAX_PRIORITY);
thread4.start();
thread5.setPriority(1);
thread5.start();
thread6.setPriority(5);
thread6.start();
thread7.setPriority(Thread.MIN_PRIORITY);
thread7.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());
}
}
守护线程
|