Java多线程(初步)
- 直接执行run()方法相当于按顺序执行了一个普通的方法,只有start()才是多线程
- 线程开启不一定马上执行,由CPU调度执行
多线程下载图片
需要导入commons-io-2.6.jar包
- 右键com->新建包(命名为lib)
- 将jar包复制到lib包
- 右键lib->添加为库
package com.wang.demo01;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class TestThread01 extends Thread{
private String url;//图片地址
private String name;//保存的文件名
public TestThread01(String name, String url) {
this.url = url;
this.name = name;
}
@Override
public void run() {
WebDownload webDownload = new WebDownload();
webDownload.downloader(url,name);
System.out.println("下载了:"+name);
}
public static void main(String[] args) {
TestThread01 testThread01 = new TestThread01("res01.jpg","http://img.desktx.com/d/file/wallpaper/scenery/20161228/3276e8be5303be3d846105e3d5a85e87.jpg");
TestThread01 testThread02 = new TestThread01("res02.jpg","http://img.desktx.com/d/file/wallpaper/scenery/20161101/81e41d07df1ea5d23c126bffa0c1521e.jpg");
TestThread01 testThread03 = new TestThread01("res03.jpg","https://pic36.photophoto.cn/20150819/0034034811946912_b.jpg");
TestThread01 testThread04 = new TestThread01("res04.png","https://pic.cnblogs.com/avatar/2621676/20211106102948.png");
testThread01.start();
testThread02.start();
testThread03.start();
testThread04.start();
}
}
//下载器
class WebDownload{
//下载方法
public void downloader(String url, String name){
try {
FileUtils.copyURLToFile(new URL(url), new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("downloader方法出现问题");
}
}
}
生产者消费者问题
管程法
package com.wang.producerConsumer;
//利用缓冲区:管程法
//生产者,消费者,缓冲区
public class TestPC {
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;
public 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 (InterruptedException e) {
e.printStackTrace();
}
}
chickens[count] = chicken;
count++;
//通知消费
this.notifyAll();
}
public synchronized Chicken pop(){
if(count == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
count--;
Chicken chicken = chickens[count];
//通知生产
this.notifyAll();
return chicken;
}
}
信号灯法(用flag判断)
package com.wang.producerConsumer;
import com.sun.org.apache.xpath.internal.operations.Bool;
public class TestPC2 {
public static void main(String[] args) {
Product2 product2 = new Product2();
new Producer2(product2).start();
new Consumer2(product2).start();
}
}
//生产者
class Producer2 extends Thread{
Product2 product2;
public Producer2(Product2 product2) {
this.product2 = product2;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if(i%2==0){
this.product2.produce("作家亲笔签名书籍");
}else {
this.product2.produce("饮料");
}
}
}
}
//消费者
class Consumer2 extends Thread{
Product2 product2;
public Consumer2(Product2 product2) {
this.product2 = product2;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
product2.consume();
}
}
}
//产品
class Product2{
boolean flag = true;
String productName;
//生产,消费者等待
//消费,生产者等待
//生产
public synchronized void produce(String productName){
if(!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.productName = productName;
System.out.println("生产了"+productName);
//通知消费
this.notifyAll();//通知唤醒
this.flag = !this.flag;
}
//消费
public synchronized void consume(){
if(flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("获得了"+this.productName);
this.notifyAll();
this.flag = !this.flag;
}
}
线程池
package com.wang.ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestPool {
public static void main(String[] args) {
//创建服务,创建线程池 newFixedThreadPool参数为线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
//执行
//execute()方法执行Runuable,
//submit()方法执行Callabel
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() {
System.out.println(Thread.currentThread().getName());
}
}
总结
package com.wang.summary;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
//回顾线程的创建
public class ThreadNew {
public static void main(String[] args) {
new MyThread1().start();
new Thread(new MyTread2()).start();
FutureTask<Integer> futureTask = new FutureTask<>(new MyThread3());
new Thread(futureTask).start();
try {
System.out.println(futureTask.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("MyThread1");
}
}
class MyTread2 implements Runnable{
@Override
public void run() {
System.out.println("MyThread2");
}
}
class MyThread3 implements Callable{
// @Override
// public Object call() throws Exception {
// return null;
// }
@Override
public Integer call() throws Exception {
System.out.println("MyThread3");
return 100;
}
}