多线程详解
Posted yangbocsu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程详解相关的知识,希望对你有一定的参考价值。
Java.Thread
【狂神说Java】多线程详解
1 线程简介
任务,进程,线程,多线程
2 线程实现(重点)
package com.yangbocsu.demo1;
//创建线程方式一:继承Thread类,重写run()方法,调用start方法开启线程
//线程开启 不一定立即执行,由cpu..
public class TestThread extends Thread {
@Override
public void run() {
//run()方法线程体
for (int i = 0; i < 20; i++) {//20.for 快速写for循环
System.out.println("我在看代码--"+i);
}
}
//主方法,
public static void main(String[] args) {
TestThread testThread1 = new TestThread();//创建一个线程对象
testThread1.start(); //调用start方法开启线程
//run方法的话 像调用run方法
//main线程,主线程
for (int i = 0; i < 2000; i++) {
System.out.println("我在学习多线程"+i);
}
}
}
调用start方法是开启一个线程,然后线程会调用run方法;
如果直接调用run方法,那和定义了一个对象,然后调用了对象中的一个方法没有区别,根本没有创建新的线程,面试题
下载别人写好的工具类库 http://commons.apache.org/proper/commons-io/
package com.yangbocsu.demo1;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//联系Thread 实现多线程同步下载图片
public class TestThread2 extends Thread{
private String url;
private String name;
public TestThread2(String url, String name) {
this.url = url;
this.name = name;
}
@Override//下载图片线程执行体
public void run() {
WebDownloader W1 = new WebDownloader();
W1.download(url,name);
System.out.println("下载的文件名为:"+name);
}
public static void main(String[] args) {
TestThread2 T1 = new TestThread2("https://img-blog.csdnimg.cn/90310cc14cfc4a3b854692694292151d.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4Njg5MjYz,size_16,color_FFFFFF,t_70","1.jpg");
TestThread2 T2 = new TestThread2("https://img-blog.csdnimg.cn/5f6937dfb9ca49b1a5d249edc791d926.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4Njg5MjYz,size_16,color_FFFFFF,t_70","2.jpg");
TestThread2 T3 = new TestThread2("https://img-blog.csdnimg.cn/3790285ed86345a1b76c59548b0c4cfb.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4Njg5MjYz,size_16,color_FFFFFF,t_70","3.jpg");
T1.start();
T2.start();
T3.start();
}
}
class WebDownloader
{
public void download(String url,String name)
{
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("Io异常,downloader方法出问题");
}
}
}
package com.yangbocsu.demo1;
//多个线程同时操作同一个对象
//买火车票的例子
//发现问题:多个线程操作同一个资源的情况下,线程不安全了,数据紊乱
public class TestThread4 implements Runnable{
private int ticketNums = 10;//票数
@Override
public void run() {
while (true)
{
if (ticketNums<1)
{
break;
}
//模拟延时
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"抢到了第"+ticketNums--+"张票");
}
}
public static void main(String[] args) {
TestThread4 t1 = new TestThread4();
new Thread(t1,"小明").start();
new Thread(t1,"老师").start();
new Thread(t1,"黄牛").start();
}
}
package com.yangbocsu.demo1;
public class Race implements Runnable {
//胜利者
private static String winner;
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
//兔子要睡觉
if(Thread.currentThread().getName().equals("兔子") && i%12==0)
{
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
boolean flag =gameOver(i);//判断比赛是否结束
if(flag) //如果比赛结束,程序就结束
{
break;
}
System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步");
}
}
//判断是否完成比赛
private boolean gameOver(int step)
{
if (winner !=null)
{
return true;
}
else {
if (step==100){
winner = Thread.currentThread().getName();
System.out.println("winner is "+winner);
return true;
}
}
return false;
}
public static void main(String[] args) {
Race race = new Race();
new Thread(race,"兔子").start();
new Thread(race,"乌龟").start();
}
}
package com.yangbocsu.demo3;
//静态代理模式总结:
//真实
public class StaticProxy {
public static void main(String[] args) {
WeddingCompany w1 = new WeddingCompany(new You());
w1.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() {
before();
this.target.HappyMarry();
after();
}
private void after() {
System.out.println("结婚之后,收尾款");
}
private void before() {
System.out.println("结婚之前,布置现场");
}
}
package com.yangbocsu.demo4;
public class TestLamdal {
//3.静态内部类
static class Like2 implements ILike{
@Override
public void lamdal() {
System.out.println("3.静态内部类");
}
}
//主函数
public static void main(String[] args) {
ILike l1= new Like();
l1.lamdal();
//3.静态内部类
ILike l2 = new Like2();
l2.lamdal();
//4.局部内部类
class Like3 implements ILike{
@Override
public void lamdal() {
System.out.println("4.局部内部类");
}
}
l2 = new Like3();
l2.lamdal();
//5.匿名内部类 没有类的名称,必须借助接口或者父类
l2 = new ILike() {
@Override
public void lamdal() {
System.out.println("5.匿名内部类");
}
};
l2.lamdal();
//6.用lambda简化
l2 = ()-> {
System.out.println("6.用lambda简化");
};
l2.lamdal();
}
}
//1.定义一个函数式接口 只有一个函数的接口
interface ILike{
void lamdal();
}
//2.实现类
class Like implements ILike{
@Override
public void lamdal() {
System.out.println("2.外部类");
}
}
带参数的:
package com.yangbocsu.demo4;
public class TestLamdal2 {
public static void main(String[] args) {
ILove t =null;
// ILove t = (int a)->{
// System.out.println("I love you -> "+a);
// };
//1.简化参数类型
t = (a) -> {
System.out.println("I love you -> "+a);
};
//2.简化括号
t = a-> {
System.out.println("I love you -> "+a);
};
//3.简化花括号
t = a->System.out.println("I love you -> "+a);
//总结:
//
t.love(66);
}
}
interface ILove
{
void love(int a);
}
3 线程状态
package com.yangbocsu.demo5;
//测试停止线程
//1.建议线程正常停止 利用次数,不建议死循环
//2.建议使用标志位--->设置一个标志位
//3.不要使用stop或者destroy等过时或者JDK不建议使用的方法
public class TestStop implements Runnable{
//1.设置一个标识位
private boolean flag = true;
@Override
public void run() {
int i = 0;
while(flag)
{
System.out.println("run ... Thread"+i++);
}
}
//2.设置一个公开的方法停止线程,转换标志位
public void stop()
{
this.flag =false;
}
public static void main(String[] args) {
TestStop testStop = new TestStop();
new Thread(testStop).start();
for (int i = 0; i < 1000; i++) {
System.out.println("main "+i);
if (i==900)
{
testStop.stop();
System.out.println("线程改停止了");
}
}
}
}
package com.yangbocsu.demo5;
public class TestYield {
public static void main(String[] args) {
MyYield myYield = new MyYield();
new Thread(myYield,"aa").start();
new Thread(myYield,"bb"20160226.CCPP体系详解(0036天)