⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tij0026.html

📁 学习java的经典书籍
💻 HTML
字号:
<html><body>

<table width="100%"><tr>
<td>
<a href="http://www.bruceeckel.com/javabook.html">Bruce Eckel's Thinking in Java</a>
</td>
<td align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0025.html">Prev</a> | <a href="tij0027.html">Next</a>
</td>
</tr></table>
<hr>

<H2 ALIGN=LEFT>
Multithreading</H2>
<DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">A
fundamental concept in computer programming is the idea of handling more than
one task at a time. Many programming problems require that the program be able
to stop what it&#8217;s doing, deal with some other problem and return to the
main process. The solution has been approached in many ways. Initially,
programmers with low-level knowledge of the machine wrote 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>interrupt
service routines
</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the suspension of the main process was initiated through a hardware
interrupt. Although this worked well, it was difficult and non-portable, so it
made moving a program to a new type of machine slow and expensive. 
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Sometimes
interrupts are necessary for handling time-critical tasks, but there&#8217;s a
large class of problems in which you&#8217;re simply trying to partition the
problem into separately-running pieces so that the whole program can be more
responsive. Within a program, these separately-running pieces are called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>threads</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
and the general concept is called 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><I>multithreading</I></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">.
A common example of multithreading is the user interface. By using threads, a
user can press a button and get a quick response rather than being forced to
wait until the program finishes its current task.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Ordinarily,
threads are just a way to allocate the time of a single processor. But if the
operating system supports multiple processors, each thread can be assigned to a
different processor and they can truly run in parallel. One of the convenient
features of multithreading at the language level is that the programmer
doesn&#8217;t need to worry about whether there are many processors or just
one. The program is logically divided into threads and if the machine has more
than one processor then the program runs faster, without any special adjustments.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">All
this makes threading sound pretty simple. There is a catch: shared resources.
If you have more than one thread running that&#8217;s expecting to access the
same resource you have a problem. For example, two processes can&#8217;t
simultaneously send information to a printer. To solve the problem, resources
that can be shared, such as the printer, must be locked while they are being
used. So a thread locks a resource, completes its task and then releases the
lock so that someone else can use the resource.
</FONT><P></DIV><DIV ALIGN=LEFT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">Java&#8217;s
threading is built into the language, which makes a complicated subject much
simpler. The threading is supported on an object level, so one thread of
execution is represented by one object. Java also provides limited resource
locking. It can lock the memory of any object (which is, after all, one kind of
shared resource) so that only one thread can use it at a time. This is
accomplished with the 
</FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black"><B>synchronized</B></FONT><FONT FACE="Carmina Md BT" SIZE=3 COLOR="Black">
keyword. Other types of resources must be locked explicitly by the programmer,
typically by creating an object to represent the lock that all threads must
check before accessing that resource.
</FONT><a name="_Toc375545205"></a><a name="_Toc408018402"></a><P></DIV>

<div align="right">
<a href="tij_c.html">Contents</a> | <a href="tij0025.html">Prev</a> | <a href="tij0027.html">Next</a>
</div>
</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -