//Thrad实现多线程
//创建线程方式一:继承Thrad类,重写run()方法,调用start开启线程
//总结:注意!!线程开启不一定立即执行,由CPU调度执行
public class TestThread1 extends Thread{
//run方法线程体
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在睡觉"+i);
}
}
//main主线程
public static void main(String[] args) {
//创建一个线程对象
TestThread1 t1 = new TestThread1( );
//调用start()方法开启线程
t1.start() ;
for (int i = 0; i < 200; i++) {
System.out.println("我在吃饭"+i);
}
}
}
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//练习Thread,实现多线程同步下载图片
public class TestThread1 extends Thread{
private String url;//网络图片地址
private String name;//保存的文件名
public TestThread1(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) {
TestThread1 t1 = new TestThread1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11355207257%2F0.jpg","1.jpg" );
TestThread1 t2 = new TestThread1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11355207257%2F0.jpg","2.jpg");
TestThread1 t3 = new TestThread1("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11355207257%2F0.jpg","3.jpg");
t1.start() ;
t2.start() ;
t3.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方法出现问题");
}
}
}
结果:
实现Runnable接口
//创建线程方式二:实现Runnable接口,重写run方法,执行线程需要丢入runnable接口实现类,调用start方法
public class TestThread2 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("我在吃饭"+i);
}
}
public static void main(String[] args) {
//创建runnable接口的实现类对象
TestThread2 t2 = new TestThread2();
//创建线程对象,通过线程对象来开启我们的线程,
new Thread(t2).start();
for (int i = 0; i < 200; i++) {
System.out.println("我在睡觉"+i);
}
}
}
下载网络图片:
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//练习Thread,实现多线程同步下载图片
public class TestThread3 implements Runnable{
private String url;//网络图片地址
private String name;//保存的文件名
public TestThread3(String url,String name){
this.url=url;
this.name=name;
}
@Override
public void run() {
WebDownLoader1 webDownLoader = new WebDownLoader1();
webDownLoader.downloader(url,name) ;
System.out.println("下载了文件名为"+name);
}
public static void main(String[] args) {
TestThread1 t1 = new TestThread1("https://img-home.csdnimg.cn/images/20220223025833.jpg","1.jpg" );
TestThread1 t2 = new TestThread1("https://img-home.csdnimg.cn/images/20220223025833.jpg","2.jpg");
TestThread1 t3 = new TestThread1("https://img-home.csdnimg.cn/images/20220223025833.jpg","3.jpg");
new Thread(t1).start() ;
new Thread(t2).start() ;
new Thread(t3).start() ;
}
}
//下载器
class WebDownLoader1{
//下载方法
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方法出现问题");
}
}
}
//多个线程同时操作同一个对象
//买火车票的例子
//发现问题:多个线程操作统一个资源的情况下,线程不安全,数据紊乱
public class TestThread4 implements Runnable{
//票数
private int ticketNums=10;
@Override
public void run() {
while(true){
if (ticketNums<=0){
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() ;
}
}
线程同步:多个线程操作同一个资源
线程同步需要队列和锁
三大不安全案例
//不安全的买票
//线程不安全:有负数
public class UnsafeBuyTicket {
public static void main(String[] args) {
BuyTicket buyTicket = new BuyTicket();
new Thread(buyTicket,"小明").start();
new Thread(buyTicket,"小红").start();
new Thread(buyTicket,"小李").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums=10;
boolean flag=true; //外部停止方式
@Override
public void run() {
//买票
while(flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void buy() throws InterruptedException {
//判断是否有票
if(ticketNums <=0){
flag=false ;
return ;
}
Thread.sleep(200) ;
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums-- );
}
}
结果:
//安全的买票
public class UnsafeBuyTicket {
public static void main(String[] args) {
BuyTicket buyTicket = new BuyTicket();
new Thread(buyTicket,"小明").start();
new Thread(buyTicket,"小红").start();
new Thread(buyTicket,"小李").start();
}
}
class BuyTicket implements Runnable{
private int ticketNums=10;
boolean flag=true; //外部停止方式
@Override
public void run() {
//买票
while(flag){
try {
buy();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//synchronized 同步方法,锁的是 thins
public synchronized void buy() throws InterruptedException {
//判断是否有票
if(ticketNums <=0){
flag=false ;
return ;
}
//模拟延时
Thread.sleep(100) ;
//买票
System.out.println(Thread.currentThread().getName()+"拿到"+ticketNums-- );
}
}
结果
JUC:
import java.util.concurrent.CopyOnWriteArrayList;
//测试JUC安全类型的集合
public class TestJUC {
public static void main(String[] args) {
CopyOnWriteArrayList<String > list = new CopyOnWriteArrayList<String >();
for (int i = 0; i < 10000; i++) {
new Thread(()->{
list.add(Thread.currentThread().getName() );
}).start();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(list.size());
}
}
结果:
线程协作:生产者消费者模式