Index 118 04 (8) 252 01 (2) Quo vadis Henryk Sienkiewicz Czarodziejska pielgrzymka Zelazny Roger Amber 05 Dworce Chaosu Sathya Sai Baba Kiedy wypełniają się proroctwa abc.com.pl 7 abc.com.pl 8 Clancy Tom Zeby Tygrysa |
[ Pobierz całość w formacie PDF ] .To suspend, the flagis set to true by calling fauxSuspend( ) and this is detectedinside run( ).The wait( ), as described earlier in thischapter, must be synchronized so that it has the object lock.InfauxResume( ), the suspended flag is set to false andnotify( ) is called—since this wakes up wait( )inside a synchronized clause the fauxResume( ) method mustalso be synchronized so that it acquires the lock before callingnotify( ) (thus the lock is available for the wait( ) towake up with).If you follow the style shown in this program you can avoid usingsuspend( ) and resume( ).[ Add Comment ]Thedestroy( ) method ofThread has never been implemented; it’s like asuspend( ) that cannot resume, so it has the same deadlock issues assuspend( ).However, this is not a deprecated method and it might beimplemented in a future version of Java (after 2) for special situations inwhich the risk of a deadlock is acceptable.[ Add Comment ]You might wonder why these methods, nowdeprecated, were included in Java in the first place.It seems an admission of arather significant mistake to simply remove them outright (and pokes yet anotherhole in the arguments for Java’s exceptional design and infallibilitytouted by Sun marketing people).The heartening part about the change is that itclearly indicates that the technical people and not the marketing people arerunning the show—they discovered a problem and they are fixing it.I findthis much more promising and hopeful than leaving the problem in because“fixing it would admit an error.” It means that Java will continueto improve, even if it means a little discomfort on the part of Javaprogrammers.I’d rather deal with the discomfort than watch the languagestagnate.[ Add Comment ]PrioritiesThepriority of a threadtells the scheduler how important this thread is.If there are a number ofthreads blocked and waiting to be run, the scheduler will run the one with thehighest priority first.However, this doesn’t mean that threads with lowerpriority don’t get run (that is, you can’t get deadlocked because ofpriorities).Lower priority threads just tend to run less often.[ Add Comment ]Although priorities are interesting toknow about and to play with, in practice you almost never need to set prioritiesyourself.So feel free to skip the rest of this section if prioritiesaren’t interesting to you.[ Add Comment ]Reading and setting prioritiesYou can read the priority of a threadwith getPriority( )and change it withsetPriority( ).Theform of the prior “counter” examples can be used to show the effectof changing the priorities.In this applet you’ll see that the countersslow down as the associated threads have their prioritieslowered://: c14:Counter5.java// Adjusting the priorities of threads.// <applet code=Counter5 width=450 height=600>// </applet>import javax.swing.*;import java.awt.*;import java.awt.event.*;import com.bruceeckel.swing.*;class Ticker2 extends Thread {private JButtonb = new JButton("Toggle"),incPriority = new JButton("up"),decPriority = new JButton("down");private JTextFieldt = new JTextField(10),pr = new JTextField(3); // Display priorityprivate int count = 0;private boolean runFlag = true;public Ticker2(Container c) {b.addActionListener(new ToggleL());incPriority.addActionListener(new UpL());decPriority.addActionListener(new DownL());JPanel p = new JPanel();p.add(t);p.add(pr);p.add(b);p.add(incPriority);p.add(decPriority);c.add(p);}class ToggleL implements ActionListener {public void actionPerformed(ActionEvent e) {runFlag = !runFlag;}}class UpL implements ActionListener {public void actionPerformed(ActionEvent e) {int newPriority = getPriority() + 1;if(newPriority > Thread.MAX_PRIORITY)newPriority = Thread.MAX_PRIORITY;setPriority(newPriority);}}class DownL implements ActionListener {public void actionPerformed(ActionEvent e) {int newPriority = getPriority() - 1;if(newPriority < Thread.MIN_PRIORITY)newPriority = Thread [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl. | |||||