JAVA study notes, java Study Notes Lin Xinliang
Previously, I learned about sequential programming. A program can only perform one step at any time. As the first class of concurrent programming, I am very excited.
1. Define a task
Implement the Runnable interface and compile the run () method (the thread will execute the code in this method ).
class LiftOff implements Runnable { protected int countDown = 10;private static int taskCount = 0;private final int id = taskCount++;public LiftOff() {}public LiftOff(int countDown) {this.countDown = countDown;}public String status() {return "#" + id + "("+ (countDown > 0 ? String.valueOf(countDown) : "Liftoff!")+ "),";}public void run() {while (countDown-- > 0) {System.out.print(status());Thread.yield();}System.out.println();}}public class ThreadTest {public static void main(String[] args) {LiftOff launch = new LiftOff();launch.run();}}/* * Output: #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(Liftoff!), */// :~
The code above exports a class from Runnable and then calls its run () method, but this method does not generate any internal thread capabilities.
To implement thread behavior, you must attach it to the thread.
2. Create and start a thread
A. Use the Thread constructor. Submit the Runnable object to a Thread constructor and call the start () method of the Thread object to start the Thread.
public class ThreadTest {public static void main(String[] args) {Thread t = new Thread(new LiftOff());t.start();System.out.println("Waiting for LiftOff");}}/* * Output: Waiting for LiftOff * #0(9),#0(8),#0(7),#0(6),#0(5),#0(4),#0(3),#0(2),#0(1),#0(Liftoff!), */// :~
In the main () thread, start () Returns quickly. At this time, the LiftOff. run () task executed by the subthread t has not ended yet. At this time, the two threads run concurrently, which can be seen through the output.
B. Use Executor. Executor is the Executor in the java. util. concurrent package, which can better manage Thread objects.
A. CachedThreadPool, which creates a thread for each task.
B. FixedThreadPool, which uses a limited thread set to execute the submitted tasks.
C. SingeThreadExecutor, for example, FixedThreadPool with a thread count of 1.
Public class ThreadTest {public static void main (String [] args) {ExecutorService exec1 = Executors. newCachedThreadPool (); ExecutorService exec2 = Executors. newFixedThreadPool (5); // The maximum number of thread sets is 5 ExecutorService exec3 = executors.newsinglethreadexecutor(executor executor exec1.exe cute (new LiftOff (); exec1.shutdown (); // prevent new tasks from being submitted to this Executors }}
3. Callable Interface
Runnable is an independent task for executing a job, but it does not return a value. Callable can return values from tasks.
The following code demonstrates its usage:
Import java. util. concurrent. *; import java. util. *; class TaskWithResult implements Callable <String> {private int id; public TaskWithResult (int id) {this. id = id;} public String call () {return "result of TaskWithResult" + id;} public class ThreadTest {public static void main (String [] args) {ExecutorService exec = Executors. newCachedThreadPool (); ArrayList <Future <String> results = new ArrayList <Future <S Tring> (); for (int I = 0; I <10; I ++) {results. add (exec. submit (new TaskWithResult (I); // The Callable call method must be ExecutorService. submit (), // At the same time, submit () will generate a Future object. You can use isDone () to query whether the Future is complete, and get the return value of call () through get .} for (Future <String> fs: results) try {System. out. print (fs. get () + ",");} catch (InterruptedException e) {System. out. println (e); return;} catch (ExecutionException e) {System. out. println (e);} fina Lly {exec. shutdown () ;}}/** Output: result of TaskWithResult 0, result of TaskWithResult 1, result of * TaskWithResult 2, result of TaskWithResult 3, result of TaskWithResult 4, result * of TaskWithResult 5, result of TaskWithResult 6, result of TaskWithResult * 7, result of TaskWithResult 8, result of TaskWithResult 9 ,*///:~
4. Thread-related methods and knowledge
A. yield () indicates that the current thread has been completed or does not need a CPU temporarily. Other threads can use it. This is only a suggestion.
B. priority. The getPriority () and setPriority () methods are used to obtain and reset the priority of a thread. The priority cannot be well mapped with most operating systems. The only thing that can be transplanted is that when the priority is set, only MAX_PRIORITY, NORM_PRIORITY, and MIN_PRIORITY are used.
C. deamon (background thread) is a general service thread provided in the background when the program is running. When all non-Background threads end, the program will kill all background threads.
To set it to a background thread, you must call setDaemon (true) before the thread starts. All the subthreads derived from the background thread are background threads.
D. join (). If a thread calls t on another thread t. join (), this thread will be suspended until the end of the target thread t can be restored (that is, t. isAlive () returns false ).
+ To be added
Who has good JAVA study notes?
It is recommended that you first learn javaSE to pass the basic learning, and then learn Servlet and mvc. Then I learned about jsp. Now I want to learn about Struts, Hibernate, Spring, and ibatis frameworks. I also need to know the most basic SQL commands. As for javaME, I don't think it is necessary if you want to do mobile development. Then you have school ME,
1. First, you must learn the basic knowledge of java.
Do not be confused by new technologies. The old technologies are based on java. If the new technologies are not well-founded, the new technologies cannot be understood.
2. Create a java Project
After learning the basic knowledge of java, make a java project to consolidate it. In the project, you will find many problems. By solving the problems, you will learn more about the basic knowledge.
3. Learn basic database knowledge and develop applications
Software development cannot leave the database, master several popular databases: Oracle, SQL server, MySQL, etc.
4. JEE Basics
First, you need to learn the website basics, including HTML, DHTML, and JavaScript. Then you need to learn XML, XML, and JAXP. Then, you need to learn the basics of JEE, including the JEE development environment, RMI/IIOP, and JNDI; finally, I learned how to develop JDBC databases.
5. web Development
Comprehensive JEE web development knowledge: Servlet + JSP + javaBean + TagLib. Here we will develop a complete web application project.
JAVA Study Notes
I have read this book
You just need to watch it.
It was written by Lin Xinliang.
Well written
I suggest you keep this book
It will be better to combine the Basic java data.