Method One: Thread class inheritance, override the run method, direct start method is called open thread.
/** * Inherit the Thread class, direct start method is called open thread. * @author LuRenJia */ public class LeaningThread extends Thread { public void run() { for (int i = 0; i < 20; i++) { System.out.println ( "But this is a cold and my video machines what to do with it?" ); } } public static void main(String[] args) { LeaningThread Lt = new LeaningThread(); Lt.start(); } }
Method Two: Implement Runable interfaces, override the run method, open thread by calling start Thread class.
/** * Implement Runnable interface to achieve multi-threaded, through a proxy * @author LuRenJia */ public class LeaningRunnable implements Runnable { public void run() { for (int i = 0; i < 20; i++) { System.out.println ( "But this is a cold and my video machines what to do with it?" ); } } public static void main(String[] args) { new Thread(new LeaningRunnable()).start(); } }
Method three: achieve Callabler interface rewrite method call, create a service ExecutorService class, call its submit method open thread, shut down services through its shutdown method.
/** * Implement Callable interface to achieve multi-threaded * @author LuRenJia */ public class LeaningCallable implements Callable<String> { private String name; public LeaningCallable(String name) { this.name = name; } // override call method, the content of a thread of execution, a return value obtained by the get method @Override public String call () throws Exception { for ( int I = 0; I <20 is; I ++ ) { System.out.println (name + ":" + "But this is a cold and my video machines what to do with it?" ); } return name; } public static void main(String[] args) { LC1 LeaningCallable = new new LeaningCallable ( "a thread" ); LeaningCallable lc2 = new LeaningCallable("线程二"); LC3 LeaningCallable = new new LeaningCallable ( "Thread Three" ); // Create a service execution ExecutorService Server = Executors.newFixedThreadPool (3 ); // submitted to the Future <String> RESULT1 = server.submit (LC1); Future<String> result2 = server.submit(lc2); Future<String> result3 = server.submit(lc3); try { TEMP String = result1.get (); // Returns a value System.out.println ( "return value is:" + TEMP); } catch (InterruptedException e) { e.printStackTrace (); } catch (ExecutionException e) { e.printStackTrace (); } // close the service server.shutdown (); } }
Create a shortcut: use lambda expressions to quickly create simple thread.
/** * Lambda expressions: When only one parent class interface method to be implemented, a lambda expression can be used to simplify the code * 1. The standard form: * ()->{ * * } Equivalent to a 2 * implements the interface subclass. * * Thread class implements Runnable interface, and this interface has a run method to be implemented. * @Author LuRenJia * */ public class LeaningLambda2 { public static void main(String[] args) { new Thread(new Runnable(){ @Override public void run() { for(int i =0;i<100;i++) { System.out.println ( "But this is a cold and my video machines what to do with it?" ); } } }).start(); // use the lambda expression to quickly create a simple thread new new the Thread (() -> { for ( int I = 0; I <100; I ++ ) { System.out.println ( "It touched my knowledge of the blind!" ); } }).start(); } }