Java Thread 2nd Edition O'REILLY
Java Threads, Second Edition
Description :
Revised and expanded to cover Java 2, Java Threads shows you how to take full advantage of Java's thread facilities: where to use threads to increase efficiency, how to use them effectively, and how to avoid common mistakes. It thoroughly covers the Thread and ThreadGroup classes, the Runnable interface, and the language's synchronized operator. The book pays special attention to threading issues with Swing, as well as problems like deadlock, race condition, and starvation to help you write code without hidden bugs.
Threads aren't a new idea: many operating systems and languages support them. But despite widespread support, threads tend to be something that everyone talks about, but few use. Programming with threads has a reputation for being tricky and nonportable. Not so with Java. Java's thread facilities are easy to use, and--like everything else in Java--are completely portable between platforms. And that's a good thing, because it's impossible to write anything but the simplest applet without encountering threads. If you want to work with Java, you have to learn about threads. This new edition shows you how to take full advantage of Java's thread facilities: where to use threads to increase efficiency, how to use them effectively, and how to avoid common mistakes. Java Threads discusses problems like deadlock, race condition, and starvation in detail, helping you to write code without hidden bugs. It brings you up to date with the latest changes in the thread interface for JDK 1.2. The book offers a thorough discussion of the Thread and ThreadGroup classes, the Runnable interface, the language's synchronized operator. It explains thread scheduling ends by developing a CPUSchedule class, showing you how to implement your own scheduling policy. In addition, Java Threads shows you how to extend Java's thread primitives. Other extended examples include classes that implement reader/writer locks, general locks, locks at arbitrary scope, and asynchronous I/O. This edition also adds extensive examples on thread pools, advanced synchronization technique, like condition variables, barriers, and daemon locks. It shows how to work with classes that are not thread safe, and pays special attention to threading issues with Swing. A new chapter shows you how to write parallel code for multiprocessor machines. In short, Java Threads covers everything you need to know about threads, from the simplest animation applet to the most complex applications. If you plan to do any serious work in Java, you will find this book invaluable. Examples available online. Covers Java 2.
Media reviews
"If you are new to Java programming and have yet to conquer threads, Java Threads is an excellent resource. You'll get a look into the basics of Java threading, and learn the techniques necessary to get your programs to function properly in a multithreaded world. Five stars" --John Zukowski, Java World, 03/2001
"This is a good book, and certainly, if you're using threads in Java non-trivially then you should have this book. Otherwise, borrow a copy and read the first half to make yourself aware of the threading issues in Java. Even better, think how your code affects others; the section on mixing threads with thread unsafe classes should make you aware of the wider issues surrounding your code, and hopefully, you'll produce well behaved classes that will be easily multi- threaded if the need arises? Do you use the collections? Think hard..."1999 --Steve Cornish, C Vu, September 1999
"A very useful well-written reference suitable for Java systems developers and as a second level text for students on computing science and IT oriented courses. Highly recommended!" --Brian Bamer, C Vu January 1998
January 1999
Pages: 336
ISBN 10: 1-56592-418-5 | ISBN 13: 9781565924185
Java™ Extreme Programming Cookbook
Brimming with over 100 "recipes" for getting down to business and actually doing XP, the Java Extreme Programming. Cookbook doesn't try to "sell" you on XP; it succinctly documents the most important features of popular open source tools for XP in Java-- including Ant, Junit, HttpUnit, Cactus, Tomcat, XDoclet-- and then digs right in,
providing recipes for implementing the tools in real-world environments.
Java™ Extreme Programming Cookbook
By Eric M. Burke, Brian M. Coyner
Publisher : O'Reilly
Pub Date : March 2003
ISBN : 0-596-00387-0
Pages : 288
Click here to download
TooltipsList Demo
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class tiplist extends JFrame {
public tiplist() {
super("ToolTip Example");
String[][] strs = { { "Item1", "HDD" },
{ "Item2", "Monitor" }, { "Item3", "Ram" },
{ "Item4", "DVD-Writer" }, { "Item5", "Microprocessor" } };
JList list = new JList(createItems(strs)) {
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 <>
ToolTipItem item = (ToolTipItem) getModel().getElementAt(
index);
return item.getToolTipText();
} else {
//return super.getToolTipText();
return null;
}
}
};
list.setToolTipText("");
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
}
Object[] createItems(String[][] strs) {
ToolTipItem[] items = new ToolTipItem[strs.length];
for (int i = 0; i <>
items[i] = new ToolTipItem(strs[i][0], strs[i][1]);
}
return items;
}
class ToolTipItem {
String obj;
String toolTipText;
public ToolTipItem(String obj, String text) {
this.obj = obj;
this.toolTipText = text;
}
public String getToolTipText() {
return toolTipText;
}
public String toString() {
return obj;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
tiplist frame = new tiplist();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(140, 150);
frame.setVisible(true);
}
}
Read more
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class tiplist extends JFrame {
public tiplist() {
super("ToolTip Example");
String[][] strs = { { "Item1", "HDD" },
{ "Item2", "Monitor" }, { "Item3", "Ram" },
{ "Item4", "DVD-Writer" }, { "Item5", "Microprocessor" } };
JList list = new JList(createItems(strs)) {
public String getToolTipText(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (-1 <>
ToolTipItem item = (ToolTipItem) getModel().getElementAt(
index);
return item.getToolTipText();
} else {
//return super.getToolTipText();
return null;
}
}
};
list.setToolTipText("");
getContentPane().add(new JScrollPane(list), BorderLayout.CENTER);
}
Object[] createItems(String[][] strs) {
ToolTipItem[] items = new ToolTipItem[strs.length];
for (int i = 0; i <>
items[i] = new ToolTipItem(strs[i][0], strs[i][1]);
}
return items;
}
class ToolTipItem {
String obj;
String toolTipText;
public ToolTipItem(String obj, String text) {
this.obj = obj;
this.toolTipText = text;
}
public String getToolTipText() {
return toolTipText;
}
public String toString() {
return obj;
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception evt) {}
tiplist frame = new tiplist();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setSize(140, 150);
frame.setVisible(true);
}
}
How to run simple JSP program
First you have to put your JSP file into the container's folder. The folder must contains a sub folder named "WEB-INF".
For an example, if we use Apache Tomcat (assume Tomcat installed to C:\Tomcat directory), you have to put your jsp file into a subfolder of C:\Tomcat\webapp folder.
Assume, your jsp file is "index.jsp"; in this case, you have to put it as:
C:\Tomcat\webapp\MyName\index.jsp
And, there should be another sub folder named "WEB-INF" as:
C:\Tomcat\webapp\MyName\WEB-INF
(However, this folder can be empty)
After that, you have to restart your web server (this is vendor specific), and you can see the output via the browser.
By default, many J2EE web servers uses 8080 port for the client access. In this case, you can see the output on your local machine by typing the following address on your browser's address bar:
http://localhost:8080/MyName
--------------------------------------...
Sample program for index.jsp
======================
<% String myName="krish"; out.println(myName); %>
<%=myName%>
The output should be:
krish krish
More info
Under the Tomcat root directory there will be a sub-directory named web-apps, all web applications running on this Tomcat server will reside there. Each web application will have it's own sub-directory under web-apps, so you could create a directory for your web application under the web-apps directory like so /web-apps/. JSP pages can be placed insisde the directory you have created or in other sub-directories you might like to create, however please note that for each new sub-directory you place your JSP files in the URL to access them will need to include each directory.
Inside the directory you created there needs to be another directory named WEB-INF, this directory will contain the web.xml file which contains configuration information for your web application such as the welcome file for your application, all servlet mappings, filters, etc...
Under the WEB-INF directory you can create another directory named classes, all your java classes used in your web application should be placed in here, in a tree structure that resembles their packages, for example: if you have a class named MyClass whose package is com.xyz then the class should be placed in /web-apps//WEB-INF/classes/com/xyz/MyClass.class. Another alternative to handling classes in this way is to package them all in a jar file, the jar file can be placed in a directory named lib also under the WEB-INF directory, for example: say you have a jar file named myLibrary.jar, it can be placed in /web-apps//WEB-INF/lib/myLibrary.jar. By placing classes and jar files in these directories Tomcat ensures they are on the CLASSPATH when you run your application.
An alternative to repeating this procedure each time you want to deploy some JSP pages to your Tomcat server is to simply create a WAR file and use the Tomcat manager (which can be accessed through a browser window) to deploy the WAR file to the server
Read more
For an example, if we use Apache Tomcat (assume Tomcat installed to C:\Tomcat directory), you have to put your jsp file into a subfolder of C:\Tomcat\webapp folder.
Assume, your jsp file is "index.jsp"; in this case, you have to put it as:
C:\Tomcat\webapp\MyName\index.jsp
And, there should be another sub folder named "WEB-INF" as:
C:\Tomcat\webapp\MyName\WEB-INF
(However, this folder can be empty)
After that, you have to restart your web server (this is vendor specific), and you can see the output via the browser.
By default, many J2EE web servers uses 8080 port for the client access. In this case, you can see the output on your local machine by typing the following address on your browser's address bar:
http://localhost:8080/MyName
--------------------------------------...
Sample program for index.jsp
======================
<% String myName="krish"; out.println(myName); %>
<%=myName%>
The output should be:
krish krish
More info
Under the Tomcat root directory there will be a sub-directory named web-apps, all web applications running on this Tomcat server will reside there. Each web application will have it's own sub-directory under web-apps, so you could create a directory for your web application under the web-apps directory like so /web-apps/. JSP pages can be placed insisde the directory you have created or in other sub-directories you might like to create, however please note that for each new sub-directory you place your JSP files in the URL to access them will need to include each directory.
Inside the directory you created there needs to be another directory named WEB-INF, this directory will contain the web.xml file which contains configuration information for your web application such as the welcome file for your application, all servlet mappings, filters, etc...
Under the WEB-INF directory you can create another directory named classes, all your java classes used in your web application should be placed in here, in a tree structure that resembles their packages, for example: if you have a class named MyClass whose package is com.xyz then the class should be placed in /web-apps//WEB-INF/classes/com/xyz/MyClass.class. Another alternative to handling classes in this way is to package them all in a jar file, the jar file can be placed in a directory named lib also under the WEB-INF directory, for example: say you have a jar file named myLibrary.jar, it can be placed in /web-apps/
An alternative to repeating this procedure each time you want to deploy some JSP pages to your Tomcat server is to simply create a WAR file and use the Tomcat manager (which can be accessed through a browser window) to deploy the WAR file to the server