JDK 1.1 for Solaris Developer's Guide

Chapter 4 Deprecated Methods

This chapter has a list of methods deprecated in JDK 1.1, and an explanation of deprecation.

What Is Deprecation?*

A method is deprecated when it is no longer considered important, and should no longer be used because it might be deleted from its class. Deprecation is a result of classes evolving, causing their APIs to change. Methods are renamed, new ones added, attributes change. Deprecated classes and methods are marked "@deprecated" in documentation comments to enable developer transition from that API to the new one. The following table lists the deprecated methods.

Table 4-1 Deprecated Methods

Class 

Method 

Replaced By 

 java.awt.BorderLayout addLayoutComponent()  addLayoutComponent(component,object)
 java.awt.CardLayout addLayoutComponent() addLayoutComponent(component,object)
 java.awt.CheckboxGroup getCurrent()  getSelectedCheckbox()
  setCurrent()  setSelectedCheckbox()
 java.awt.Choice countItems() getItemCount()
 java.awt.Component getPeer() No replacement.
  enable() setEnabled(true)
  disable() setEnabled(false)
  show() setVisible(true)
  hide() setVisible(false)
  location() getLocation()
  move() setLocation()
  size() getSize()
  resize() setSize()
  bounds() getBounds()
  reshape() setBounds()
  preferredSize() getPreferredSize()
  minimumSize() getMinimumSize()
  layout() doLayout()
  inside() contains()
  locate() getComponentAt()
  deliverEvent() dispatchEvent()
  postEvent() dispatchEvent()
  handleEvent() processEvent()
  mouseDown() processMouseEvent()
  mouseDrag() processMouseMotionEvent()
  mouseUp() processMouseEvent(MouseEvent)
  mouseMove() processMouseMotionEvent()
  mouseEnter() processMouseEvent()
  mouseExit() processMouseEvent()
  keyDown() processKeyEvent()
  keyUp() processKeyEvent()
  action()Register as ActionListener on component firing action events.
  gotFocus() processFocusEvent()
  lostFocus() processFocusEvent()
  extFocus() transferFocus()
 java.awt.Container countComponents() getComponentCount()
  insets() getInsets()
  preferredSize() getPreferredSize()
  minimumSize() getMinimumSize()
  deliverEvent() dispatchEvent()
  locate() getComponentAt()
 java.awt.FontMetrics getMaxDescent() getMaxDescent()
 java.awt.Frame setCursor()setCursor()method in Component
  getCursorType()getCursor() method in Component
 java.awt.Graphics getClipRect() getClipBounds()
 java.awt.List countItems() getItemCount()
  clear() removeAll()
  isSelected() isIndexSelected()
  allowsMultipleSelections() isMultipleMode()
  setMultipleSelections() setMultipleMode()
  preferredSize() getPreferredSize()
  minimumSize() getMinimumSize()
  delItems()No longer for public use; retained as package private.
 java.awt.Menu countItems() getItemCount()
 java.awt.MenuBar countMenus() getMenuCount()
 java.awt.MenuComponents getPeer() No replacement.
  postEvent() dispatchEvent()
 java.awt.MenuContainer postEvent() dispatchEvent()
 java.awt.MenuItem enable() setEnabled(true)
  disable() setEnabled(false)
 java.awt.Polygon getBoundingBox() getBounds()
  inside() contains()
 java.awt.Rectangle reshape() setBounds()
  move() setLocation()
  resize() setSize()
  inside() contains()
 java.awt.ScrollPane layout() doLayout()
 java.awt.Scrollbar getVisible() getVisibleAmount()
  setLineIncrement() setUnitIncrement()
  getLineIncrement() getUnitIncrement()
  setPageIncrement() setBlockIncrement()
  getPageIncrement() getBlockIncrement()
 java.awt.TextArea insertText() insert()
  appendText() append()
  replaceText() replaceRange()
  preferredSize() getPreferredSize()
  minimumSize() getMinimumSize()
 java.awt.TextField setEchoCharacter() setEchoChar()
  preferredSize() getPreferredSize()
  minimumSize() getMinimumSize()
 java.awt.Window postEvent() dispatchEvent()
 java.io. ByteArrayOutputStream toString() toString(String enc) or toString(), which uses the platform's default character encoding
 java.io.DataInputStream readLine() BufferedReader.readLine()
 java.io.PrintStream printStream() PrintWriter class
 java.io.StreamTokenizer streamTokenizer() Convert input stream to character stream
 java.lang.Character isJavaLetter() isJavaIdentifierStart(char)
  isJavaLetterOrDigit() isJavaIdentifierPart(char)
  isSpace() isWhitespace(char)
 java.lang.ClassLoader defineClass() defineClass (java.lang.String,byte[],int,int)
 java.lang.Runtime getLocalizedInputStream() InputStreamReader and BufferedReader classes
  getLocalizedOutputStream() Use OutputStreamWriter, BufferedWriter, and PrintWriter classes
 java.lang.String string() Use String constructors that take a character-encoding name or use default encoding.
  getBytes() getBytes(String enc) or getBytes()
 java.lang.System getenv() Use java.lang.System.getProperty methods' system properties and corresponding get TypeName methods of Boolean, Integer, and Long primitive types.
 java.lang.Thread resume()Refer to "Deprecated Threads Methods"
 java.lang.Thread stop()Refer to "Deprecated Threads Methods"
 java.lang.Thread suspend()Refer to "Deprecated Threads Methods"
 java.util.Date getYear() Calendar.get(Calendar.YEAR)-1900
  setYear() Calendar.set(Calendar.YEAR+1900)
  getMonth() Calendar.get(Calendar.MONTH)
  setMonth() Calendar.set(Calendar.MONTH,int month)
  getDate() Calendar.get(Calendar.DAY_OF_MONTH)
  setDate() Calendar.set(Calendar.DAY_OF_MONTH,int date)
  getDay() Calendar.get(Calendar.DAY_OF_WEEK)
  getHours() Calendar.get(Calendar.HOUR_OF_DAY)
  setHours() Calendar.set(Calendar.HOUR_OF_DAY,int hours)
  getMinutes() Calendar.get(Calendar.MINUTE)
  setMinutes() Calendar.set(Calendar.MINUTE,int minutes)
  getSeconds() Calendar.get(Calendar.SECOND)
  setSeconds() Calendar.set(Calendar.SECOND,int seconds)
  parse() DateFormat.parse(String s)
  getTimezoneOffset() Calendar.get(Calendar.ZONE_OFFSET) +Calendar.get(Calendar.DST_OFFSET)
  toLocaleString() DateFormat.format(Date date)
  toGMTString() DateFormat.format(Date date) using a GMT TimeZone
  UTC() Calendar.set (year+1900,month,date,hrs,min,sec) or GregorianCalendar (year+1900,month,date,hrs,min,sec), using a UTC TimeZone, followed by Calendar.getTime().getTime().

Deprecated Threads Methods

The Thread.stop, Thread.suspend, and Thread.resume methods are deprecated as of JDK 1.1. Thread.stop is being deprecated because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors was in an inconsistent state, other threads might view these objects in an inconsistent state. Such objects are said to be damaged.

Threads operating on damaged objects can behave arbitrarily, either obviously or not. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that the program might be corrupted. The corruption can manifest itself at an unpredictable time after the damage occurs.

Substitute any use of Thread.stop with code that provides for a gentler termination. Most uses of stop() can and should be replaced by code that modifies a variable indicating that the target thread should stop running. The target thread should check this variable regularly. If the variable indicates that the thread is to stop, the thread should then return from its run() method in an orderly fashion. For example, suppose your applet contains the following start(), stop(), and run() methods:


public void start() {
		blinker = new Thread(this);
		blinker.start();
}
public void stop() {
		blinker.stop();
// UNSAFE!
	}
public void run() {
		Thread thisThread = Thread.currentThread();
		while (true) {
			try {
				Thread.sleep(interval);
			}
			catch (InterruptedException e){
			}
			repaint();
		}           
	}

You can avoid the use of Thread.stop by replacing the applet's stop() and run() methods with:


public void stop() {
		blinker = null;
}            
public void run() {
		Thread thisThread = Thread.currentThread();
		while (blinker == thisThread) {
			try {
				Thread.sleep(interval);
			}
			catch (InterruptedException e){
			}
			repaint();
		}
}

Thread.suspend is inherently deadlock-prone, so it is also being deprecated. Thus, the deprecation of Thread.resume is also necessary. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume(), deadlock results.

Such deadlocks typically manifest themselves as frozen processes. As with Thread.stop, the prudent approach is to have the target thread poll a variable indicating the desired state of the thread (active or suspended). When the correct state is suspended, the thread waits using Object.wait. When the thread is resumed, the target thread is notified using Object.notify. For example, suppose your applet contains the following mousePressed event handler, which toggles the state of a thread called blinker:


public void mousePressed(MouseEvent e) {
		e.consume();
		if (threadSuspended)
			blinker.resume();
		else
			blinker.suspend();
// DEADLOCK-PRONE!
		threadSuspended = !threadSuspended;
}

You can avoid the use of Thread.suspend and Thread.resume by replacing the event handler above with:


public synchronized void mousePressed(MouseEvent e) {
		e.consume();
		threadSuspended = !threadSuspended;
		if (!threadSuspended)
			notify();
}

and adding the following code to the run loop:


synchronized(this) {
		while (threadSuspended)
			wait();
}

The wait() method throws the InterruptedException, so it must be inside a try ... catch clause. You can also put it in the same clause as the sleep. The check should follow (rather than precede) the sleep so the window is immediately repainted when the thread is resumed. The resulting run() method follows:


public void run() {
		while (true) {
			try {
				Thread.sleep(interval);
				synchronized(this) {
					while (threadSuspended)
						wait();
				}
			}
			catch (InterruptedException e){
			}
			repaint();
		}
} 

The notify() in the mousePressed() method and the wait() in the run() method are inside synchronized blocks. This is required by the language, and ensures that wait() and notify() are properly serialized. In practical terms, this eliminates race conditions that could cause the suspended thread to miss a notify() and remain suspended.