Create a thread of three ways:
(1) Thread class inheritance
(2) implement Runnable
(3) implement the interface Callable
Code Example:
1, inheritance Thread class
. 1 Package com.sxt.thread; 2 . 3 / ** . 4 * @ClassName StartThread . 5 * Create a thread system: 6 * 1. Create: Thread + override inherited RUN . 7 * 2. Start: Create a subclass object Start + . 8 * / . 9 public class StartThread the extends the thread { 10 . 11 / ** 12 is * @Description thread entry point 13 is * @date 2019-07-23 16:04 14 * @Param [] 15 * @return void 16 * * / . 17 @Override 18 public void RUN () { . 19 for ( int I = 0; I <20 is; I ++ ) { 20 is System.out.println ( "while listening to music" ); 21 is } 22 is } 23 is 24 public static void main (String [] args) { 25 // create a sub-class object 26 StartThread ST = new new StartThread (); 27 // start 28 st.start (); // not guaranteed to run immediately, called by the CPU 29 // st.run (); // common method invocation 30 for ( int0 = I; I <20 is; I ++ ) { 31 is System.out.println ( "Code write side" ); 32 } 33 } 34 }
2, implement Runnable
. 1 Package com.sxt.thread; 2 . 3 / ** . 4 * @ClassName StartThread . 5 * create a thread way: 6 * 1. Create: to achieve the Runnable + rewritable RUN . 7 * 2. Start: Create an object class that implements the Thread objects + + Start . 8 * . 9 * recommended: avoiding the limitations of single inheritance, preferentially using the interface 10 * facilitate sharing resources . 11 * / 12 is public class StartRun the implements the Runnable { 13 is 14 / ** 15 * @Description thread entry point 16 * 2019- @date 16:04 07-23 . 17 * @Param [] 18 is * @returnvoid . 19 * * / 20 is @Override 21 is public void RUN () { 22 is for ( int I = 0; I <20 is; I ++ ) { 23 is System.out.println ( "while listening to music" ); 24 } 25 } 26 is 27 public static void main (String [] args) { 28 / * 29 // Create a class object implement 30 StartRun new new StartRun SR = (); 31 is // create the proxy class object 32 the Thread new new T = the Thread (SR); 33 is // start up 34 t.start (); // not guaranteed to run immediately, by the CPU calls 35 * / 36 // If an object is only used once, can be created using anonymous 37 new new the Thread ( new new StartRun ()) Start ().; 38 is // st.run (); // common method calls 39 for ( int I = 0; I <20 is; I ++ ) { 40 System.out.println ( "Code write side" ); 41 } 42 } 43 }
to sum up:
Example:
- Create a target audience: IDownloader id = new IDownloader ( "Picture address", "baidu.png");
- + Create a Thread object associated with the target object: Thread t = new Thread (id);
- Start threads: t.start ()
3, implement Callable Interface
. 1 Package com.sxt.thread; 2 . 3 Import the java.util.concurrent *. ; . 4 . 5 / ** . 6 * understand how to create a thread three: (the JUC) . 7 * Callable implement interface methods call override 8 * 9 * / 10 public class CDownloader the implements a Callable <Boolean> { . 11 12 is Private String URL; // remote path 13 is Private String name; // store name 14 15 public CDownloader (URL String, String name) { 16 the this.url = url; 17 this.name = name; 18 } 19 20 @Override 21 public Boolean call() throws Exception { 22 23 WebDownloader wd = new WebDownloader(); 24 wd.download(url, name); 25 System.out.println("图片名称" + name); 26 return true; 27 } 28 29 public static void main(String[] args) throws ExecutionException, InterruptedException { 30 31 CDownloader cd1 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/20190603145749911985.jpg", "20190603145749911985.jpg"); 32 CDownloader cd2 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/201906031457493181984.jpg", "201906031457493181984.jpg"); 33 CDownloader cd3 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/201906031457505691446.jpg", "201906031457505691446.jpg"); create execution services://36start three threads //3534 37 ExecutorService ser = Executors.newFixedThreadPool(3); 38 //3.提交执行: 39 Future<Boolean> result1 = ser.submit(cd1); 40 Future<Boolean> result2 = ser.submit(cd2); 41 Future<Boolean> result3 = ser.submit(cd3); 42 //4.获取结果: 43 boolean r1 = result1.get(); 44 boolean r2 = result2.get(); 45 boolean r3 = result3.get(); 46 System.out.println(r3); 47 //5.关闭服务: 48 ser.shutdownNow(); 49 } 50 }