20165336 2017-2018-2 "Java Programming" Week 8 Learning Summary

20165336 2017-2018-2 "Java Programming" Week 8 Learning Summary

Textbook learning content summary

Chapter 12

1. Program : A piece of static code. Process : A dynamic execution process of a program, which corresponds to a complete process from code loading, execution to execution completion. This process is also the process itself from generation, development to death.

2. Thread status and life cycle : new, running, interrupted, and dead:

  • New: When an Threadobject of a class or its subclasses is declared and created, the new thread object is in the new state. At this point it already has the corresponding memory space and other resources.
  • Operation: 1. If the thread is created by a subclass of Thread, the run()method in this class is executed immediately, and the program must override the method of the parent class in the subclass run(). 2. Before the thread does not end the run()method, do not let the thread call start()the method again, otherwise an IllegalThreadStateExceptionexception will occur
  • Interruption: There are 4 reasons for interruption:
    The JVM switches the CPU resources from the current thread to other threads, so that the thread gives up the right to use the CPU and is in an interrupted state.
    While the thread is using CPU resources, the sleep(int millsecond) method is executed to make the current thread go to sleep.
    While the thread is using CPU resources, the wait() method is executed.
    While the thread is using CPU resources, it executes an operation and enters the blocking state.
  • Death: The so-called dead state is that the thread releases the entity, that is, the memory allocated to the thread object is released, and the thread in the dead state does not have the ability to continue running

3. Common methods of threads

  • start()Method: Only newly created threads can call this method.
  • run()Method: Whether the method ends is an important criterion for judging whether the thread ends. Only when the method finishes running, the thread can call the start() method again.
  • sleep(int millsecond)Method: A thread can call this method to put the program to sleep and "wake up" until the number of seconds specified by the parameter has elapsed. This method must be called in a try-catch statement, because an InterruptedException will be thrown if the thread is interrupted while sleeping.
  • isAlive()Method: This method is used to determine whether the thread is running, if so, return true, if not, return false. Note that an already running thread does not assign entities to the thread without entering the dead state.
  • currentThread()The method is a class method in the Thread class that returns the thread currently using the CPU.
  • interrupt()The method is used to "wake up" a thread that was sleeping by calling the sleep method.

Problems and solutions in code debugging

  • Question 1: When writing a four arithmetic calculator, input (multiplication sign) in the windows command window *, and an exception is reported, but the input 带引号的乘号“*”runs normally.

  • Solution: According to Baidu's search and answer , it is known that when running a java program under the windows console, the input command line *(作为乘号使用)must be written in "*"this form.
  • Question 2: The array calls the length method without (), otherwise an error will occur.

code hosting

(screenshot of the running result of the statistics.sh script)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324681813&siteId=291194637