方式一、继承Thread类(不建议使用)
public class TestThread extends Thread{
@Override
public void run() {
for (int i=0; i < 10 ;i++){
System.out.println("我重写了父类的run方法"+i);
}
}
public static void main(String[] args) {
TestThread testThread = new TestThread();
testThread.start();
for (int i=0; i<1000;i++){
System.out.println("我是主线程:"+i);
}
}
}
多线程实践
public class DownLoadThread extends Thread{
private String imgPath;
private String imgName;
@Override
public void run() {
new downLoadImg().downLoad(imgPath,imgName);
System.out.println("下载图片:"+imgName);
}
public DownLoadThread(String url, String name){
this.imgName = name;
this.imgPath = url;
}
public static void main(String[] args) {
DownLoadThread thread1 = new DownLoadThread("https://pics1.baidu.com/feed/29381f30e924b89958a9c20b2150539d0b7bf6cf?token=eeb60e52d837786bcf41302053a10f4f&f=jpeg","1.jpg");
DownLoadThread thread2 = new DownLoadThread("https://pics1.baidu.com/feed/ac345982b2b7d0a259ff4fc284b938014b369a1d?token=c70d71cee868911e7a66dee6a16b56ae&f=jpeg","2.jpg");
DownLoadThread thread3 = new DownLoadThread("https://pics2.baidu.com/feed/2cf5e0fe9925bc31869a10fe1089c3b9ca13706b?token=36d26770e3253b52e617b40e5da1dfd8&f=jpeg","3.jpg");
thread1.start();
thread2.start();
thread3.start();
}
}
class downLoadImg{
public void downLoad(String url, String name) {
try{
URL url1 = new URL(url);
FileUtils.copyURLToFile(url1,new File(name));
}catch (IOException e){
e.printStackTrace();
}
}
}
方式二、实现Runnable接口(建议使用)
public class RunnableThread implements Runnable{
public void run() {
for (int i=0; i < 10 ;i++){
System.out.println("我重写了父类的run方法"+i);
}
}
public static void main(String[] args) {
RunnableThread testThread = new RunnableThread ();
new Thread(testThread).start();
for (int i=0; i<1000;i++){
System.out.println("我是主线程:"+i);
}
}
}
方式三、实现Callable接口(了解即可)
public class CallableThread implements Callable<Object> {
private String imgPath;
private String imgName;
public Object call() throws Exception {
new downLoadImg2().downLoad(imgPath,imgName);
System.out.println("下载图片:"+imgName);
return null;
}
public CallableThread(String url, String name){
this.imgName = name;
this.imgPath = url;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
CallableThread thread1 = new CallableThread("https://pics1.baidu.com/feed/29381f30e924b89958a9c20b2150539d0b7bf6cf?token=eeb60e52d837786bcf41302053a10f4f&f=jpeg","1.jpg");
CallableThread thread2 = new CallableThread("https://pics1.baidu.com/feed/ac345982b2b7d0a259ff4fc284b938014b369a1d?token=c70d71cee868911e7a66dee6a16b56ae&f=jpeg","2.jpg");
CallableThread thread3 = new CallableThread("https://pics2.baidu.com/feed/2cf5e0fe9925bc31869a10fe1089c3b9ca13706b?token=36d26770e3253b52e617b40e5da1dfd8&f=jpeg","3.jpg");
ExecutorService service = Executors.newFixedThreadPool(3);
Future<Object> submit1 = service.submit(thread1);
Future<Object> submit2 = service.submit(thread2);
Future<Object> submit3 = service.submit(thread3);
Object o1 = submit1.get();
Object o2 = submit2.get();
Object o3 = submit3.get();
service.shutdownNow();
}
}
class downLoadImg2{
public void downLoad(String url, String name) {
try{
URL url1 = new URL(url);
FileUtils.copyURLToFile(url1,new File(name));
}catch (IOException e){
e.printStackTrace();
}
}
}