Java Notes-6

- The  wait()

and  notify() methods of the  Object class extend this capability by allowing us to

explicitly coordinate the waiting and running threads.

-By executing  wait() from a synchronized block, a thread gives up its hold on the lock

and goes to sleep. A thread might do this if it needs to wait for something to happen in

another part of the application, as we’ll see shortly.

-Later, when the necessary event

happens, the running thread calls  notify() from a block synchronized on the same

object

-A  ThreadLocal is an object

wrapper that automatically maintains a separate value for any thread calling it

-At any given time, a thread is in one of five general states that encompass its lifecycle

and activities.

-NEW

The thread has been created but not yet started.

-RUNNABLE

The normal active state of a running thread, including the time when a thread is

blocked in an I/O operation, like a read or write or network connection.

-BLOCKED

The thread is blocked, waiting to enter a synchronized method or code block. This

includes the time when a thread has been awakened by a  notify() and is attempting

to reacquire its lock after a  wait() .

-WAITING, TIMED_WAITING

The thread is waiting for another thread via a call to  wait() or  join() . In the case

of  TIMED_WAITING , the call has a timeout.

-TERMINATED

The thread has completed due to a return, an exception, or being stopped.

-In a time-sliced system,

thread processing is chopped up so that each thread runs for a short period of time

before the context is switched to the next thread,

-Higher-priority threads still preempt lower-priority threads in this scheme. The addi‐

tion of time-slicing mixes up the processing among threads of the same priority;

-The subtleties relating to priority and performance relate to

how Java threads and priorities are mapped to real threads in the OS. For this reason,

thread priorities should be reserved for system and framework development.

-Whenever a thread sleeps, waits, or blocks on I/O, it gives up its time slot and another

thread is scheduled.

-The  ThreadGroup class allows us to deal with threads wholesale: we can use it to arrange

threads in groups and deal with the groups as a whole.

-For example, we can forbid threads in a particular group from

interacting with threads in other groups. This is one way web browsers can prevent

threads started by Java applets from stopping important system threads.

ThreadGroup

myTaskGroup = new ThreadGroup("My Task Group");

Thread myTask = new Thread( myTaskGroup, taskPerformer );

-The  ThreadGroup class exists so that you can control threads in batches. It has methods

that parallel the basic  Thread control methods—even the deprecated  stop() ,  sus

pend() , and  resume()

-We can set the maximum priority for threads created in a thread group by calling

setMaximumPriority() .

-Finally, you can get a list of all threads in a group. The method  activeCount() tells you

how many threads are in the group;

-We can handle uncaught excep‐

tions for a single thread like this:

-An alternative approach is to create “thread pools” where a fixed number of threads pull

tasks from a queue and return for more when they are finished.

-he  java.util.concurrent package and

subpackages introduced with Java 5.0 build on this functionality, adding important

threading utilities and codifying some common design patterns by supplying standard

implementations

-One such related pair of patterns is the concept

of an executor service that manages tasks and that of a thread pool that services tasks in

an efficient way.

时间: 2024-08-14 00:43:03

Java Notes-6的相关文章

Java Notes 00 - Singleton Pattern(单例总结)

转:http://hukai.me/java-notes-singleton-pattern/ 这里不赘述单例模式的概念了,直接演示几种不同的实现方式. 0)Eager initialization 如果程序一开始就需要某个单例,并且创建这个单例并不那么费时,我们可以考虑用这种方式: 1 2 3 4 5 6 7 8 9 public class Singleton { private static final Singleton INSTANCE = new Singleton(); priva

编写高质量代码改善java程序的151个建议——[1-3]基础?亦是基础

原创地址:   http://www.cnblogs.com/Alandre/  (泥沙砖瓦浆木匠),需要转载的,保留下! Thanks The reasonable man adapts himself to the world;the unreasonable one persists in trying to adapt the world to himself. -萧伯纳 相信自己看得懂就看得懂了,相信自己能写下去,我就开始写了.其实也简单-泥沙砖瓦浆木匠 Written In The

JAVA编程思想(4) - 多态(一)

多态 在面向对象的程序设计语言中,多态是继数据抽象和继承之后的第三种基本类型. 多态通过分离做什么和怎么做,从另一个角度将接口和实现分离开来.多态不但能够改善代码的组织结构和可读性,还能够创建可扩展程序. 再论向上转型 代码 //: polymorphism/music/Note.java // Notes to play on musical instruments. package polymorphism.music; public enum Note { MIDDLE_C, C_SHAR

Git学习6:Git冲突模拟与解决

在真实的Git运行环境中,往往涉及多个用户对版本仓库的协作,而每个用户都有一个完整的Git版本仓库副本,所以在把各自的操作结果推送到远程仓库的时候出现冲突的可能性就非常高. 在Git中解决冲突的一个优雅方式是:首先通过命令git fetch获取远程仓库最新的修改,然后执行命令git merge将本地的操作结果(实际上就是一个commit)与远程仓库的修改(远程仓库最新的commit)进行合并,如果在合并的过程没有发生冲突,那么Git会生成一个新的commit,并自动提交.但是,合并并非总是成功的

88. Notes客户端开发中的Java

回顾一下,在Lotus Notes杂谈专栏中,我陆陆续续写的关于Notes里Java编程的文章也有不少:6. Lotus Notes中的开发语言.8. 从Lotusscript到Java.22. 为什么要在XPages中写Java?.31. 怎样在Xpages应用中写Java.42. Lotus Notes中的垃圾回收之Java等等.这篇文章继续谈谈Notes客户端开发中的Java.用Java进行Notes界面操作?Notes作为一个软件和开发平台,从历史上看总的发展趋势是可编程性不断提高.R4

Read Notes:[Effective Java] Consider static factory methods instead of Constructors

Providing a static method instead of a public constructor has both advantages and disadvantages. One advantage of static factory methods is that, unlike constructors,they have names. A Second advantage of static factory methods is that, unlike constr

Java 8 Learn Notes - Streams

Main reference [1] http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples 1. How Streams Work A stream represents a sequence of elements and supports different kind of operations to perform computations upon those elements: List<String>

Java learning notes (1):Basic Knowlege points

Basic Knowlege points: 1: it's necessary that there is only one public class in per .java file 2: .java file name should better to same as class name 3: when set the environment variable CLASSPATH, you should make sure the '.' path can't be lack of.

java 命令notes

jps:查看本机的Java中进程信息. jstack:打印线程的栈信息,制作线程Dump. jmap:打印内存映射,制作堆Dump. jstat:性能监控工具. jhat:内存分析工具. jconsole:简易的可视化控制台. jvisualvm:功能强大的控制台. 1.jps 2.jstack http://www.hollischuang.com/archives/110 3.jmap 4.jstat( http://www.hollischuang.com/archives/481 ) j

Java Basic Notes——static修饰符

1.static 在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只要程序在运行,那么这块内存就会一直存在.这样做有什么意义呢? 在Java程序里面,所有的东西都是对象,而对象的抽象就是类,对于一个类而言,如果要使用他的成员,那么普通情况下必须先实例化对象后,通过对象的引用才能够访问这些成员,但是有种情况例外,就是该成员是用static声明的(在这里所讲排除了类的访问控制). static 是