多线程
线程:继承thread类 重写run()方法 调用start开启线程
public class Test extends Thread{
@Override
public void run(){
for (int i = 0; i < 1000; i++) {
System.out.println("多线程"+i);
}
}
public static void main(String[] args) {
Test thread1 = new Test();
thread1.start();
for (int i = 0; i < 1000; i++) {
System.out.println("主线程"+i);
}
}
}
运行出来是穿插的
下载网图
先导入lib的commons-io.jar包
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class Test extends Thread{
//public class Test implements Runnable{
private String url;
private String name;
public Test(String url,String name){
this.url = url;
this.name = name;
}
@Override
public void run(){
WebDownloader webDownloader = new WebDownloader();
webDownloader.downloader(url,name);
System.out.println("下载了文件名"+name);
}
public static void main(String[] args) {
Test test = new Test("http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1606/30/c3/23589301_1467290861869_800x800.jpg","test.jpg");
test.start();
//new Thread(test).start();接口方法
}
}
//下载器
class WebDownloader{
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常 Downloader Error");
}
}
}
接口
- extends Thread
- implements Runnable(建议)
将上述代码改为implements接口:
extends Thread --> implements Runnable
test.start() --> new Thread(test,name).start()
线程
线程可能出现数据紊乱
Thread.currentThread().getName();//String获取线程名
Thread.sleep(1000);//停顿
callable好处:
- 可以抛出异常
- 可以返回返回值
静态代理
真实对象和代理对象都要实现同一个接口
代理对象要代理真是角色
代理对象可以做真实对象做不了的事情
真实对象专注于做自己的事情
//静态代理
public class Test {
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("HappyMarry");
}
}
class WeddingCompany implements Marry{
private Marry target;
public WeddingCompany(Marry target){
this.target = target;
}
@Override
public void happyMarry() {
before();
this.target.happyMarry();
after();
}
private void after() {
System.out.println("after suffer");
}
private void before() {
System.out.println("before happy");
}
}
lambda表达式
new Thread((参数)->System.out.println("lambda表达式")).start();
避免匿名内部类定义过多
任何接口 如果只包含唯一一个抽象类方法 那么它就是一个函数式接口
五种定义类的方式
public class Test{
public static void main(String[] args) {
Like1 like1 = new Like1();
like1.lambda();
Like2 like2 = new Like2();
like2.lambda();
//③局部内部类
class Like3 implements ILike{
@Override
public void lambda() {
System.out.println("this is lambda③");
}
}
Like3 like3 = new Like3();
like3.lambda();
//④匿名内部类
ILike like4 = new ILike(){
@Override
public void lambda() {
System.out.println("this is lambda④");
}
};
like4.lambda();
//⑤lambda表达式
ILike like5 = ()->{
System.out.println("this is lambda⑤");
};
like5.lambda();
}
//②静态内部类
static class Like2 implements ILike{
@Override
public void lambda() {
System.out.println("this is lambda②");
}
}
}
//①函数式接口
class Like1 implements ILike{
@Override
public void lambda() {
System.out.println("this is lambda①");
}
}
interface ILike{
void lambda();
}
线程状态
线程停止:
- 建议线程正常停止—>利用次数 不建议死循环
- 建议使用标志位—>设置一个标志位
- 不要使用stop或者destory等过时的方法
public class Test implements Runnable{
private boolean flag = true;
public static void main(String[] args) {
Test test = new Test();
new Thread(test).start();
for (int i = 0; i <= 1000; i++) {
System.out.println("main running"+i);
}
test.stop();
}
public void stop(){
flag = false;
}
@Override
public void run() {
int i=0;
while(flag){
System.out.println("running..."+i++);
}
}
}
网络延时:放大问题的发生性
获取系统时间
import java.util.Date;
import java.text.SimpleDateFormat;
public class Test {
public static void main(String[] args) {
Date startTime = new Date(System.currentTimeMillis());//获得系统当前时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println(simpleDateFormat.format(startTime));
}
}
礼让/插队/状态
Thread.yield();//线程礼让
Test test = new Test();
Thread thread = new Thread(test);//代理
thread.start();
thread.join();//合并线程(插队)
Thread.State state = thread.getState();//枚举
thread.start();
state = thread.getState();
sout(state);
terminated的线程无法再次启动
守护线程/优先级
setPriority(1~10);
thread.setDaemon(true);
虚拟机不用等待守护线程执行完毕
如后台记录日志 内存监控 垃圾回收
线程同步
synchronize 关键字 同步方法
public class Test{
public static void main(String[] args) {
TicketStation station = new TicketStation();
new Thread(station,"A").start();
new Thread(station,"B").start();
new Thread(station,"C").start();
}
}
class TicketStation implements Runnable{
private int ticketNum = 10;
@Override
public synchronized void run() {
while(ticketNum>0){
buy();
}
}
private void buy(){
System.out.println(Thread.currentThread().getName()+"买到了"+ticketNum);
ticketNum--;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
同步块
synchronized(obj);//锁变化的量
死锁
public class Test {
public static void main(String[] args) {
Func func1 = new Func(true);
Func func2 = new Func(false);
new Thread(func1).start();
new Thread(func2).start();
}
}
class No1{
}
class No2{
}
class Func implements Runnable{
static No1 no1 = new No1();
static No2 no2 = new No2();
boolean flag;
Func(boolean flag){
this.flag=flag;
}
@Override
public void run() {
if(flag){
synchronized (no1){
System.out.println(this.flag+"获得no1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (no2){
System.out.println(this.flag+"获得no2");
}
}
}
else {
synchronized (no2){
System.out.println(this.flag+"获得no2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (no1){
System.out.println(this.flag+"获得no1");
}
}
}
}
}
Lock
import java.util.concurrent.locks.ReentrantLock;
public class Test {
public static void main(String[] args) {
Fun fun1 = new Fun("A");
new Thread(fun1).start();
new Thread(fun1).start();
new Thread(fun1).start();
}
}
class Fun implements Runnable{
static int ticketNum = 10;
String name;
private final ReentrantLock lock =new ReentrantLock();
Fun(String name){
this.name=name;
}
@Override
public void run() {
while(true){
try{
lock.lock();
if(ticketNum>0) {
Thread.sleep(1000);
System.out.println(name + "票数" + ticketNum--);
}
else break;
}catch (Exception e){
}
finally {
lock.unlock();
}
}
}
}
线程通信
wait()等待
notify()唤醒
缓冲法
public class Test{
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Producer(container).start();
new Consumer(container).start();
}
}
//生产者
class Producer extends Thread{
SynContainer container;
public Producer(SynContainer container){
this.container = container;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("生产了"+i+"只鸡");
container.push(new Chicken(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer container;
public Consumer(SynContainer container){
this.container = container;
}
@Override
public void run(){
for (int i = 0; i < 100; i++) {
System.out.println("消费了"+container.pop().id+"只鸡");
}
}
}
//产品
class Chicken{
int id;
Chicken(int id){
this.id = id;
}
}
//缓冲区
class SynContainer{
//容器
Chicken[] chickens = new Chicken[10];
int count = 0;
public synchronized void push(Chicken chicken){
if(count == chickens.length){
try{
this.wait();
}catch (Exception e){
e.printStackTrace();
}
}
chickens[count]=chicken;
count++;
this.notifyAll();
}
public synchronized Chicken pop(){
if(count==0){
try{
this.wait();
}catch (Exception e){
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
this.notifyAll();
return chicken;
}
}
信号灯法
public class Test{
public static void main(String[] args) {
TV tv = new TV();
new Player(tv).start();
new Watcher(tv).start();
}
}
class Player extends Thread{
TV tv;
public Player(TV tv){
this.tv= tv;
}
@Override
public void run(){
for (int i = 0; i < 20; i++) {
if(i%2==0){
this.tv.play("节目A");
}else{
this.tv.play("节目B");
}
}
}
}
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv= tv;
}
@Override
public void run(){
for (int i = 0; i < 20; i++) {
this.tv.watch();
}
}
}
class TV{
String program;
boolean flag = true;
public synchronized void play(String program){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("演员表演了"+program);
this.notifyAll();
this.program=program;
this.flag=!this.flag;
}
public synchronized void watch(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众观看了"+program);
this.notifyAll();
this.flag=!this.flag;
}
}
线程池
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Test {
public static void main(String[] args) {
//创建线程池 10
ExecutorService service = Executors.newFixedThreadPool(10);
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭连接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
}
总结
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//总结
public class Test{
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> futureTask = new FutureTask(new MyThread3());
new Thread(futureTask).start();
try {
Integer integer = futureTask.get();
System.out.println(integer);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
//继承Thread类
class MyThread1 extends Thread{
@Override
public void run(){
System.out.println("MyThread1");
}
}
//实现Runnable接口
class MyThread2 implements Runnable{
@Override
public void run(){
System.out.println("MyThread2");
}
}
//实现Callable接口
class MyThread3 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
}