Java Notes-5
and some new syntax to limit the parameter types that may be applied to a generic type.
class EmployeeList< T extends Employee & Ranked & Printable > { ... }
-As we’ll see in this section, wildcards are Java’s way of introducing polymorphism into
the type parameter portion of the generic equation.
-The unbounded wildcard instantiation is a kind of supertype of all of these concrete
instantiations.
-We saw the extends construct used to specify an upper bound for both type variables
and wildcard instantiations. It implies a type that is “at the top” of the object hierarchy
for the bound.
-Generic methods can
appear in any class (not just generic classes) and are very useful for a wide variety of
applications.
-Like generic classes, generic methods have a parameter type declaration using the <>
syntax. This syntax appears before the return type of the method:
-In general, wildcard instantiations of generics can be used as the base type for arrays in
the same way that concrete instantiations can. Let’s look at an example:
-All execution in Java is associated with a Thread object, beginning with a “main” thread
that is started by the Java VM to launch your application. A new thread is born when
we create an instance of the java.lang.Thread class
-Any class that contains an appropriate run() method can declare that it implements the
Runnable interface. An instance of this class is then a runnable object that can serve as
the target of a new thread.
-A newly born thread remains idle until we give it a figurative slap on the bottom by
calling its start() method. The thread then wakes up and proceeds to execute the run()
method of its target object. start() can be called only once in the lifetime of a thread.
-. We could also
start the thread without saving a reference to it if we won’t be using it later:
-The methods wait() and join() coordinate the execution of two or more threads.
We’ll discuss them in detail when we talk about thread synchronization later in this
chapter.
? The interrupt() method wakes up a thread that is sleeping in a sleep() or wait()
operation or is otherwise blocked on a long I/O operation.
-Finally, if you need to coordinate your activities with another thread by waiting for it
to complete its task, you can use the join() method.
-A thread continues to execute until one of the following happens:
? It explicitly returns from its target run() method.
? It encounters an uncaught runtime exception.
? The evil and nasty deprecated stop() method is called.
- thread can live on, even after what is ostensibly the
part of the application that created it has finished.
-the Devil thread sets its daemon status when it is created. If any Devil
threads remain when our application is otherwise complete, the runtime system kills
them for us.
-The AWT thread is not a daemon thread, so it doesn’t
exit automatically when other application threads have completed, and the developer
must call System.exit() explicitly.
-Applets are embeddable Java applications that are expected to start and stop themselves
on command, possibly many times in their lifetime.
-or example, suppose we implemented a SpeechSynthesizer class that contains a say()
method. We don’t want multiple threads calling say() at the same time because we
wouldn’t be able to understand anything being said.
-if one thread were changing the values of the variables in
setRow() at the same moment another thread was reading the values in sumRow() . To
prevent this, we have marked both methods as synchronized .
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。