- 浏览: 614306 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
月光杯:
问题解决了吗?
Exceptions in HDFS -
iostreamin:
神,好厉害,这是我找到的唯一可以ac的Java代码,厉害。
[leetcode] word ladder II -
standalone:
One answer I agree with:引用Whene ...
How many string objects are created? -
DiaoCow:
不错!,一开始对这些确实容易犯迷糊
erlang中的冒号 分号 和 句号 -
standalone:
Exception in thread "main& ...
one java interview question
Previously
in this chapter, you learned that each thread has an assigned priority
that is used to let more important threads use resources ahead of
lower-priority resources. Priority is used as a guide for the operating system
to determine which thread gets accesses to a resource such as the CPU.
In reality, an operating system takes other factors into consideration.
Typically, programmers have little or no control over those other
factors. Therefore, they establish a priority for their threads without
further concern over those other factors. A priority is an integer from 1 to 10 inclusive, where 10 is the highest priority, referred to as the maximum priority
, and 1 is the lowest priority, also known as the minimum priority.
The normal priority is 5, which is the default priority for each thread. In
general, a thread with a higher priority bumps a thread with a lower
priority from using a resource. The lower-priority thread pauses until
the higher-priority thread is finished using the resource. Whenever two
threads of equal priority need the same resource, the thread that
accesses the resource first has use of the resource. What happens to
the second thread depends on the operating system under which your
program is running. Some operating systems
force the second thread to wait until the first thread is finished with
the resource. Other operating systems require the first thread to give
the second thread access to the resource after a specified time period.
This is to ensure that one thread doesn’t hog a resource and prevent
other threads from utilizing it. In the real world,
the first thread usually pauses while using the resource because
another resource it needs isn’t available. It is during this pause that
the operating system has the first thread relinquish the resource. The
problem is, you don’t know if and when the pause will occur. It is best
to always cause a thread to pause periodically whenever the thread is
using a resource for a long period time. In this way, the thread shares
the resource with other threads. You learn how to pause a thread in the
“Suspending and Resuming a Thread” section of this chapter. You
need to keep in mind that there is a downside to periodically pausing a
thread. Pausing a thread diminishes the performance of your program and
could cause a backlog for use of the resource. Therefore, you need to
monitor the performance of your program regularly to make sure you are
not experiencing this negative aspect of pausing a thread. For
now let’s focus on something you do have control over—setting the
priority of a thread. You set a thread’s priority by calling the setPriority()
method, which is defined in the Thread
class. The setPriority()
method requires one parameter, which is the integer representing the
level of priority. You have two ways in which to represent the
priority. You can use an integer from 1 to 10, or you can use final
variables defined in the Thread
class. These variables are MIN_PRIORITY
, MAX_PRIOIRTY
, and NORM_PRIOIRTY
. You can determine the priority level of a thread by calling the getPriority()
method, which is also defined in the Thread
class. The getPriority()
method does not requires an argument, and it returns the integer representing the level of priority for the thread. The following example illustrates how to use the setPriority()
and getPriority()
methods. This example creates two child threads and sets the priority
for each. First, the low-priority thread starts, followed by the
high-priority thread. Here’s what is displayed when you run this
program (notice that the high-priority thread runs ahead of the
low-priority thread, even though the low-priority thread started first):
low priority started
Multithreading in Java - Setting Thread Priorities
(Page 7 of 10 )
NOTE:
The results displayed on your screen might be different from the
results shown here because of the way your operating system handles
thread priorities.
high priority started
high priority running.
low priority running.
low priority stopped.
high priority stopped.
class MyThread implements Runnable {
Thread t;
private volatile boolean running = true;
public MyThread (int p, String tName) {
t = new Thread(this,tName);
t.setPriority (p);
}
public void run() {
System.out.println(t.getName() + " running.");
}
public void stop() {
running = false;
System.out.println(t.getName() + " stopped.");
}
public void start() {
System.out.println(t.getName() + " started");
t.start();
}
}
class Demo {
public static void main(String args[] ) {
Thread.currentThread().setPriority(10);
MyThread lowPriority =
new MyThread (3, "low priority");
MyThread highPriority =
new MyThread (7, "high priority");
lowPriority.start();
highPriority.start();
try {
Thread.sleep(1000);
} catch ( InterruptedException e) {
System.out.println("Main thread interrupted.");
}
lowPriority.stop();
highPriority.stop();
try {
highPriority.t.join();
lowPriority.t.join();
} catch (InterruptedException e) {
System.out.println(
"InterruptedException caught");
}
}
}
发表评论
-
ssl 与 java 实例
2014-01-27 10:10 863http://www.iteye.com/topic/1125 ... -
Java NIO
2014-01-10 21:28 771看了这个java nio的教程,明白了什么是Selector. ... -
再谈Java的wait(), sleep(), notify()和notifyAll()
2013-07-25 10:59 1971一段时间不用java,这些概念就全混淆了,有必要彻底澄清一下, ... -
Why singleton is anti-pattern?
2013-07-03 10:12 916OO Test Other reasons? -
How to generate the serialVersionUID when you implement Serializable interface,j
2013-07-01 10:52 989http://docs.oracle.com/javase/6 ... -
Java Override的两个问题
2013-06-01 11:40 10121: 如果子类中的方法的参数是父类的方法的子类型,那么算不算o ... -
Java常用类API统计
2013-06-01 11:35 0String charAt(int) compareTo( ... -
How many string objects are created?
2013-06-01 10:18 1363This is a very common java inte ... -
使用Java的DelayQueue容易碰到的一个坑
2013-05-27 17:32 6822今天不忙,学习一下java.util.concurrent.D ... -
[leetcode] Balanced Binary Tree
2013-04-28 14:08 1618Check if a binary tree is balan ... -
[leetcode] find median of two sorted arrays
2013-04-26 10:55 1505http://leetcode.com/onlinejudge ... -
[leetcode] word ladder
2013-04-25 15:05 2316Q: Given two words (start and ... -
[leetcode] word ladder II
2013-04-15 07:35 12119http://leetcode.com/onlinejudge ... -
[leetcode] Count and Say
2013-04-12 14:05 2283http://leetcode.com/onlinejudge ... -
Date/Time处理函数总结 [To Do]
2013-04-12 10:46 729几种我所用到的用来处理日期,时间的函数总结。 Perl 1 ... -
[leetcode] Palindrome Partition
2013-04-12 10:25 1348http://leetcode.com/onlinejudge ... -
[leetcode] Palindrome Partitioning II
2013-04-11 16:45 1557http://leetcode.com/onlinejudge ... -
Profiling your Java code using Spring
2013-03-05 15:02 737Quite good article!!! http://w ... -
Java的Generics的几点限制
2012-12-28 15:00 4824参见 http://docs.oracle.com/ ... -
Overriding Method Using Parameter That is a Subclass?
2012-12-27 22:14 934参见 http://www.coderanch.com/t/3 ...
相关推荐
Multithreading in Java Chapter 9. Input/Output, Serialization and Cloning Chapter 10. Generics, java.util and other API Chapter 11. Network Programming Chapter 12. Applets Chapter 13. Event Handling ...
其中,"multithreading applications in Win32.pdf"可能是一个关于Windows平台下多线程应用的补充材料,可能出自另一本书或文章,但与《Thinking in Java》的主题相关。多线程是Java编程中的一个重要部分,它允许...
- **Concurrency:** Explore concurrency concepts and multithreading in Java. #### Conclusion The 1Z0-811 exam is a significant step towards achieving Oracle Java SE certification. By following a ...
Java多线程 多线程是Java的一项功能,它允许并发执行程序的两个或更多部分,以最大程度地利用CPU。 这种程序的每个部分都称为线程。 因此,线程是进程中的轻量级进程。 可以使用两种机制来创建线程: 扩展Thread类...
实践中的Java并发和多线程 这是发行的的代码存储库。 它包含从头到尾完成视频课程所需的所有支持项目文件。 关于视频课程 多核处理器无处不在-从超级计算机到随身携带的移动设备。 这就是为什么现代开发人员必须知道...
In 1999, with his wife Luda and two daughters, he emigrated to the USA and has been living in Colorado ever since, working as a Java programmer. In his free time, Nick likes to write and hike in the ...
It also discusses about exception handling, multithreading and java libraries. Further, it explains how to interact with client side applications like applets and handling events. The last section ...
Chapter 3 Fundamental Programming Structures in Java(新增批注共44条) Chapter 4 Objects and Classes(新增批注共55条) Chapter 5 Inheritance(新增批注共42条) Chapter 6 Interfaces and Inner Classes...
s built-in support for multithreading through the Thread class and the Runnable interface. By creating and starting multiple threads, a Java application can perform several operations simultaneously, ...
This book was written with one goal in mind: to provide Java programmers with the expertise needed to build efficient, scalable Java code. The author shares his experience in server-side performance ...
If Java is the heavy metal of computer programming, then Python is the jazz that opens doors of freedom in software development. Both Java and Python are object-oriented programming languages. Both ...
Carver和Kuo-Chung Tai撰写,由John Wiley & Sons, Inc.出版,是一本深入探讨多线程编程的专著,尤其聚焦于Java和C++(包括Pthreads和Win32)语言环境下的多线程程序设计、测试与调试。本书不仅适合初学者作为入门...
This chapter introduces multithreading concepts and techniques in Java, including thread creation, synchronization, and inter-thread communication. Multithreading is particularly useful for improving...
9. **多线程(Multithreading)**:Java支持多线程编程,书中会讲解线程的创建、同步和通信,以及线程池的使用。 10. **输入/输出流(I/O Stream)**:介绍如何在Java中进行文件读写,网络通信以及其他类型的输入...
3 FUNDAMENTAL PROGRAMMING STRUCTURES IN JAVA 4 OBJECTS AND CLASSES 5 INHERITANCE 6 INTERFACES AND INNER CLASSES 7 GRAPHICS PROGRAMMING 8 EVENT HANDLING 9 USER INTERFACE COMPONENTS WITH SWING 10 ...
Core Java offers a total immersion approach for CC++ programmer wanting to learn java. The course will use JDK(Java Development Kit) and covers all the ...Chapter 13 Managing Input/Output Files in Java
Chapter 3 Fundamental Programming Structures in Java(新增批注共44条) Chapter 4 Objects and Classes(新增批注共55条) Chapter 5 Inheritance(新增批注共42条) Chapter 6 Interfaces and Inner Classes...
6. ** 示例代码 **:在`producer_consumer_using_multithreading_in_java-master`这个项目中,应该包含了一个完整的示例,演示了如何用Java实现生产者消费者模型。源代码可能包括一个`Producer`类和一个`Consumer`类...