In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the way to create threads?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Thread, Runnable and Callable
There are two ways to create a new thread of execution. One is to declare a class as a subclass of Thread. This subclass should override the method Thread of the run class. You can then assign and start an instance of the subclass. For example, a thread that calculates primes greater than a specified value can be written as follows:
The first way for package com.example.thread.class1;// to implement multithreading is to inherit the Thread class, rewrite run, and call the start method to start the thread public class TestThread1 extends Thread {@ Override public void run () {/ / thread method body for (int I = 0; I).
< 2000; i++) { System.out.println("我在看代码****"+i); } }public static void main(String[] args) {//创建一个线程对象 TestThread1 thread1 = new TestThread1(); thread1.start(); // 主线程 for (int i = 0; i < 2000; i++) { System.out.println("我在学习多线程****"+i); } }} 线程开启不一定立即执行,由cpu调度实现多线程下载图片package com.example.thread.class1;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;// 实现多线程同步下载图片 2public class TestThread2 extends Thread { String url; // 网络图片地址 String name; // 保存文件名 public TestThread2(String url,String name){this.url =url; this.name =name; }@Override public void run() { downLoader loader = new downLoader(); try { loader.downLoad(url,name); System.out.println("下载的图片"+name); } catch (MalformedURLException e) { e.printStackTrace(); } }public static void main(String[] args) { TestThread2 thread1 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","1"); TestThread2 thread2 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","2"); TestThread2 thread3 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","3"); thread1.start(); thread2.start(); thread3.start(); }}// 下载器 WebdownLoaderclass downLoader{public void downLoad(String url,String name) throws MalformedURLException {try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,downloader 方法出现问题"); } }} 另一种方法来创建一个线程是声明实现类Runnable接口。 那个类然后实现了run方法。 然后可以分配类的实例,在创建Thread时作为参数传递,并启动。 这种其他风格的同一个例子如下所示: package com.example.thread.class1;import org.apache.commons.io.FileUtils;import java.io.File;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;// 实现多线程同步下载图片 2public class TestThread2 implements Runnable { String url; // 网络图片地址 String name; // 保存文件名 public TestThread2(String url,String name){this.url =url; this.name =name; }@Override public void run() { downLoader loader = new downLoader(); try { loader.downLoad(url,name); System.out.println("下载的图片"+name); } catch (MalformedURLException e) { e.printStackTrace(); } }public static void main(String[] args) { TestThread2 thread1 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","1"); TestThread2 thread2 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","2"); TestThread2 thread3 = new TestThread2("https://img-pre.ivsky.com/img/tupian/pre/202103/04/sunyunzhu_heise_lianyiqun-006.jpg","3"); new Thread(thread1).start(); new Thread(thread2).start(); new Thread(thread3).start(); }}// 下载器 WebdownLoaderclass downLoader{public void downLoad(String url,String name) throws MalformedURLException {try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,downloader 方法出现问题"); } }} 多线程中的静态代理模式 1.真实角色(委托对象) 2.代理角色 3.共同实现的接口 package com.example.demo.class1;import java.lang.reflect.WildcardType;/** * 静态代理模式: * 真实对象和代理对象都要同时实现同一个接口 * * 好处: * 代理对象可以做很多真实对象做不了的事情 * 真实对象就专注自己的事情 */public class StacticProxy { public static void main(String[] args) {// new Thread( ()->System.out.println ("I love you"). Start (); new WeddingCompany (new You ()). HappyMarry (); / * WeddingCompany weddingCompany = new WeddingCompany (new You ()); weddingCompany.HappyMarry (); * /}} interface Marry {void HappyMarry () } / / the real character, you go to marry class You implements Marry {@ Override public void HappyMarry () {System.out.println ("Mr. Chen is getting married.");}} / / proxy role, help you marry class WeddingCompany implements Marry {private Marry target; / / the real object public WeddingCompany (Marry target) {this.target = target } @ Override public void HappyMarry () {before (); this.target.HappyMarry (); / / this is the real marriage after ();} private void after () {System.out.println ("collect money after marriage");} private void before () {System.out.println ("decorate the scene before marriage.") This is the end of the content of "how threads are created". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.