The DateApp Application
import java.util.Date;
class DateApp {
public static void main (String args[]) {
Date today = new Date();
System.out.println(today);
}
}
The "Hello World" Application
class HelloWorldApp {
public static void main (String args[]) {
System.out.println("Hello World!");
}
}
À§ ÇÁ·Î±×·¥Àº "Hello World" ApplicationÀ» º¯Çü½ÃŲ ÇÁ·Î±×·¥ÀÌ´Ù.
±½Àº ±Û¾¾·Î Ç¥ÇöµÈ ºÎºÐÀÌ ¼·Î ´Ù¸¥ ºÎºÐÀÌ´Ù.
DateApp´Â Date class¿¡¼ ±â´ÉÀ» °¡Á®¿Â´Ù.
ApplicationÀ̸§Àº 'DateApp' vs 'HelloWorldApp'·Î ´Ù¸£´Ù.
DateApp´Â Date object¸¦ »ý¼ºÇÏ°í ¼·Î´Ù¸¥ Ãâ·ÂÀ» ÇÑ´Ù.
'import'¶ó´Â ºÎºÐÀº java.util package¿¡¼ Date class¸¦ °¡Á®¿Í
»ç¿ëÇÑ´Ù´Â ÀǹÌÀÌ´Ù. java.utilÀº ´Ù¾çÇÑ ¿©·¯°¡Áö class¸¦ ¸ð¾Æ³õÀº
packageÀÌ´Ù. Date class´Â system°ú´Â º°µµ·Ï ³¯Â¥¸¦ ó¸®ÇÒ ¼ö ÀÖµµ·Ï
¸¸µç classÀÌ´Ù.
2.2.2 Ŭ·¹½º Á¤ÀÇÇϱâ.
Java language¿¡¼, ¸ðµç function°ú variableÀº class(or object)¾ÈÂʺκп¡
Á¤ÀǸ¦ ÇÑ´Ù. Java language´Â Àü¿¬º¯¼ö³ª, ÇÔ¼ö¸¦ Çã¿ëÇÏÁö ¾Ê´Â´Ù.
µû¶ó¼ Java applicationÀº class¸¦ Á¤ÀÇÇÔÀ¸·Î¼ ½ÃÀ۵ȴÙ.
import java.util.Date;
class DateApp {
public static void main(String args[]) {
Date today = new Date();
System.out.println(today);
}
}
±½Àº ±Û¾¾°¡ DateApp applicationÀÇ
'class Á¤ÀÇ ¿µ¿ª(class definition block)'ÀÌ´Ù.
Java languageÀÇ ±âº» ±¸Á¶ÀÎ class´Â data³ª classÀÇ instanceÀÇ behaviourÀ»
Ç¥ÇöÇØÁÖ´Â templateÀÌ´Ù.
Class¸¦ instantiate½ÃÅ°¸é object¸¦ »ý¼º½ÃÅ°´Â °ÍÀÌ´Ù.
Class³ª object¿¡ °ü·ÃµÈ data¸¦ variableÀ̶ó°í ÇÏ°í, behaviour¸¦
method¶ó°í ÇÑ´Ù.
class name {
. . .
}
±âº»ÀûÀÎ class Á¤ÀÇÀÇ Çü½ÄÀº À§¿Í °°´Ù.
2.2.3 'main()'¸Þ¼Òµå.
Java applicationÀÇ ÁßÃß´Â main() methodÀÌ´Ù.
Java interpreterÇÏ¿¡¼ Java applicationÀ» ¼öÇà½Ãų¶§, ½ÇÇà ½ÃÄÓ·Á´Â
classÀÇ À̸§À» ¸í½ÃÇØ¾ß ÇÑ´Ù. ±×·¯¸é interpreter´Â ±× classÀÇ
main() method¸¦ È£ÃâÇÑ´Ù. main() method´Â ÇÊ¿äÇÑ resource¸¦ ÇÒ´çÇÏ°í
´Ù¸¥ method¸¦ ½ÇÇà½ÃÅ°´Â µî programÀÇ È帧À» ÄÁÆ®·ÑÇÑ´Ù.
import java.util.Date;
class DateApp {
public static void main(String args[]) {
Date today = new Date();
System.out.println(today);
}
}
À§ÀÇ DateAppó·³ Java application´Â ²À main() method¸¦ °¡Áö°í
ÀÖ¾î¾ß¸¸ ÇÑ´Ù. ±×Ç¥ÇöÀº ´ÙÀ½°ú °°´Ù.
public static void main(String args[])
'public'Àº main() method°¡ ¾î¶² object¿¡¼µçÁö ¿ÜºÎ¿¡¼ È£Ãâ µÉ ¼ö
ÀÖÀ½À» ³ªÅ¸³»´Â°ÍÀÌ´Ù.
'static'Àº main() method°¡ static methodÀÓÀ» ³ªÅ¸³»´Â °ÍÀÌ°í,
void´Â return value°¡ ¾ø´Ù´Â °ÍÀ» ³ªÅ¸³½´Ù.
Java languageÀÇ main() method´Â C/C++ ¿¡¼ÀÇ main()ÇÔ¼ö¿Í ºñ½ÁÇÏ´Ù.
C/C++¿¡¼ runtime systemÀº programÀÇ main() functionÀ» È£ÃâÇÔÀ¸·Î¼
½ÃÀ۵ȴÙ. main() functionÀº ´Ù¸¥ functionÀ» È£ÃâÇϰԵȴÙ.
¸¶Âù°¡Áö·Î, Java language¿¡¼µµ, Java interpreter¿¡¼ class¸¦ ½ÇÇà½Ãų¶§
runtime systemÀº classÀÇ main() method¸¦ È£ÃâÇÔÀ¸·Î½á ½ÃÀ۵ȴÙ.
±×¸®°í main() method´Â ´Ù¸¥ method¸¦ È£ÃâÇÑ´Ù.
¸¸¾à main() method°¡ ¾ø´Â class¸¦ Java interpreter·Î ½ÇÇà ½Ãų·Á°í Çϸé
¿¡·¯ ¸Þ½ÃÁö¸¦ È£¸é¿¡ º¸¿©ÁÖ°Ô µÈ´Ù.
main() methodÀÇ arguments
main() methodÀÇ argument´Â ´ÙÀ½°ú ¸¶Âù°¡Áö·Î StringÀÇ ¹è¿·Î
Çϳª¸¦ ¹Þ´Â´Ù.
public static void main(String args[])
ÀÌ StringÀÇ ¹è¿Àº runtime system¿¡¼ application¿¡ Á¤º¸¸¦ ³Ñ°ÜÁÖ±âÀ§ÇÑ
¼ö´ÜÀ¸·Î ¾²ÀδÙ. ¹è¿¿¡ ÀÖ´Â String°ªÀ» command line argument¶ó°í ºÎ¸¥´Ù.
»ç¿ëÀÚ´Â command line argument ¸¦ ÅëÇؼ application¿¡ Á¤º¸¸¦ Àü´ÞÇÒ
¼ö ÀÖ´Ù. ´ÙÀ½ ÇÁ·Î±×·¥Àº ±× ¿¹¸¦ º¸¿©ÁÖ°í ÀÖ´Ù.
class Echo {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
DOS prompt»ó¿¡¼ ´ÙÀ½°ú °°ÀÌ ½ÇÇà ½Ãų ¼ö ÀÖ´Ù.
C:\> java Echo Drink Hot Java
Drink
Hot
Java
ÇÏÁö¸¸ ´ÙÀ½°ú °°ÀÌ ÀÔ·ÂÇϸé space¸¦ ¹«½ÃÇÏ°Ô µÇ¹Ç·Î argument´Â 1°³ÀÎ ¼ÀÀÌ´Ù.
% java Echo "Drink Hot Java"
Drink Hot Java
Unix¿¡´Â word arguments, arguments that require arguments, flags
3°¡Áö Á¾·ùÀÇ command line argument°¡ ÀÖ´Ù.
±× 3°¡Áö argument¸¦ ´ÙÀ½ÀÇ ¿¹¿¡¼ ó·³ Java¿¡¼ ±¸ÇöÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½Àº ÀϹÝÀûÀÎ command line ÀÔ·Â ÇüÅ´ ´ÙÀ½°ú °°´Ù.
usage: application_name [ optional_args ] required_args
´ÙÀ½Àº ÇÁ·Î±×·¥°ú »ç¿ë¹ýÀ» º¸¿©ÁÖ°í ÀÖ´Ù.
usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename
class ParseCmdLine {
public static void main(String args[]) {
int i = 0, j;
String arg;
char flag;
boolean vflag = false;
String outputfile = "";
while (i < args.length && args[i].startsWith("-")) {
arg = args[i++];
// word argument
// use this type of check for "wordy" arguments
if (arg.equals("-verbose")) {
System.out.println("verbose mode on");
vflag = true;
}
// use this type of check for arguments that require arguments
else if (arg.equals("-output")) {
if (i < args.length)
outputfile = args[i++];
else
System.err.println("-output requires a filename");
if (vflag)
System.out.println("output file = " + outputfile);
}
// use this type of check for a series of flag arguments
else {
for (j = 1; j < arg.length(); j++) {
flag = arg.charAt(j);
switch (flag) {
case 'x':
if (vflag) System.out.println("Option x");
break;
case 'n':
if (vflag) System.out.println("Option n");
break;
default:
System.err.println("ParseCmdLine: illegal option " + flag);
break;
}
}
}
}
if (i == args.length)
System.err.println("Usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename");
else
System.out.println("Success!");
}
}
JavaÀÇ command line argument´Â C/C++°ú´Â ´Ù¸£´Ù.
C/C++Àº argc¿Í argv¸¦ ³Ñ°ÜÁÖÁö¸¸, Java´Â argsÇϳª¸¸À» ³Ñ°ÜÁØ´Ù.
´ÙÀ½Àº C/C++ÀÇ argumentÇü½Ä°ú JavaÀÇ argumentÇü½Ä°ú ºñ±³ÇÏ¿´´Ù.
diff file1 file2
C/C++ argvÀÇ Ã¹¹ø° command line argumentsms diffÀÌ´Ù.
java diff file1 file2
JavaÀÇ Ã¹¹ø° command line argumentsms file1ÀÌ´Ù.
Runtime systemÀº Command line argumentÀ» ¿ÀÁ÷ Java application¿¡¸¸
³Ñ°ÜÁØ´Ù. ´ë½Å Java appletÀº parameter¸¦ »ç¿ëÇϴµ¥ ±×Çü½ÄÀº ´Ù¸£´Ù.
2.2.4 Java °´Ã¼ÀÇ ¼Ò°³.
Java applicationÀÇ ÁÖ¿ä ±¸¼ºÀº Á¦°øµÇ´Â object, class, method,
ÀÚ½ÅÀÌ Á÷Á¢ ÀÛ¼ºÇÑ Java language statement·Î ±¸¼ºµÈ´Ù.
´ÙÀ½ÀÇ DateApp´Â °£´ÜÇÑ Java programÀ¸·Î, °£´ÜÇϱ⠶§¹®¿¡ ´õÀÌ»óÀÇ
class¸¦ Á¤ÀÇÇÒ ÇÊ¿ä´Â ¾øÁö¸¸, ´ëºÎºÐÀÇ ÇÁ·Î±×·¥¿¡¼ Á»´õ º¹ÀâÇÏ°í,
´Ù¸¥ class¸¦ Á¤ÀÇÇؾßÇÏ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇØ¾ß ÇÒ°ÍÀÌ´Ù.
import java.util.Date;
class DateApp {
public static void main(String args[]) {
Date today = new Date();
System.out.println(today);
}
}
2.2.4.1 °´Ã¼ÀÇ ¼±¾ð°ú ÃÊÄ¡È.
main() methodÀÇ Ã¹¹ø° ÁÙ¿¡¼ today¶ó´Â object¸¦ ¼±¾ð, ÃʱâÈ, instantiate
½ÃŲ´Ù. todayÀÇ constructor¿¡¼ ÇöÀç ½Ã°£°ú ³¯Â¥·Î ÃʱâÈÇÑ´Ù.
ObjectÀÇ ¼±¾ðÀº ´ÙÀ½°ú °°´Ù.
Date today;
±× ÀϹÝÀûÀÎ ÇüÅ´ ´ÙÀ½°ú °°´Ù.
type name
new ¿¬»êÀÚ´Â ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏ¿© Object¸¦ »ý¼º(instantiating)ÇÑ´Ù.
ObjectÀÇ ÃʱâÈ´Â new ¿¬»êÀÚ°¡ »ý¼ºµÇ´Â ObjectÀÇ constructor method¸¦
È£ÃâÇÏ¸é¼ ÀÌ·ç¾îÁø´Ù. µû¶ó¼ constructor´Â Ãʱâȸ¦ ÇØ¾ß ÇÑ´Ù.
´ÙÀ½°ú °°ÀÌ argument°¡ ¾ø´Â constructor¸¦ default constructor¶ó°í ÇÑ´Ù.
Date()
±×·¯³ª, class´Â ¿©·¯°³ÀÇ constructor¸¦ °¡Áú ¼ö ÀÖ´Ù. ´Ü À̸§Àº ¸ðµÎ
°°¾Æ¾ß ÇÏÁö¸¸, argumnetÀÇ typeÀ̳ª, °¹¼ö´Â ¼·Î ´Þ¶ó¾ßÇÑ´Ù.
¿¹¸¦ µé¸é ´ÙÀ½°ú °°´Ù.
Date(int year, int month, int day)
À§ÀÇ °ÍÀº DateÀÇ ³â, ¿ù, ÀÏÀ» ÃʱâÈ ½Ãų ¼ö ÀÖ´Â constructorÀÌ´Ù.
2.2.4.2 ÀνºÅϽº¿Í ½ºÅÍƽ.
´ÙÀ½ÀÇ Java statement¿¡¼
System.out.println(today);
System.outÀº System classÀÇ out variableÀ» ÂüÁ¶ÇÑ´Ù.
C/C++ÀÇ structÀÇ element¸¦ ÂüÁ¶ÇϵíÀÌ Java¿¡¼µµ '.'À» ÀÌ¿ëÇؼ
classÀÇ static variableÀ̳ª method¸¦ ÂüÁ¶ ÇÒ ¼ö ÀÖ´Ù.
ÁÖÀÇÇÒÁ¡Àº, application¿¡¼ System class¸¦ instantiate½ÃÅ°Áö ¾Ê°í
out variableÀº class·Î ºÎÅÍ Á÷Á¢ ÂüÁ¶ µÇÁö ¾Ê´Â´Ù´Â°ÍÀÌ´Ù.
¿Ö³ÄÇϸé, outÀº static variable·Î ¼±¾ð µÇ¾î Àֱ⠶§¹®ÀÌ´Ù.
¶ÇÇÑ staticÀ¸·Î ¼±¾ðµÈ method¸¦ static method¶ó°í ºÎ¸¥´Ù.
staticÀ¸·Î ¼±¾ðµÇÁö ¾ÊÀº method¿Í variableÀ» instance method,
instance variableÀ̶ó°í ÇÑ´Ù. Instance variableÀ̳ª method¸¦
»ç¿ëÇϱâ À§Çؼ´Â, ¿ì¼± class¸¦ instanticate½ÃÅ°°í,
instance·ÎºÎÅÍ method¿Í variableÀ» ¾ò¾î³»¾ßÇÑ´Ù.
SystemÀÇ out variableÀº java.io package¿¡ ÀÖ´Â PintStream classÀÇ
instance·Î½á, standard output streamÀ» ó¸®ÇÏ´Â ObjectÀÌ´Ù.
System class°¡ application¿¡ loadµÉ ¶§, SystemÀº outÀ» »ý¼º½ÃÅ°°í,
±×¹ÛÀÇ ´Ù¸¥ static variableµµ »ý¼ºÇÑ´Ù. println()Àº outÀÇ
instance method¶ó°í ºÎ¸¥´Ù.
Static variable°ú method¸¦ class variable, class method¶ó°í ºÎ¸¥´Ù.
¿Ö³ÄÇϸé, °¢°¢ÀÇ class variable°ú class method ¿ÀÁ÷ Çϳª¸¸ »ý¼ºµÇ¾î¼
°øÀ¯¸¦ ÇÏÁö¸¸, instance method¿Í instance vairableÀº classÀÇ instance¸¶´Ù
Çϳª¾¿ °¢°¢ µû·Î »ý¼ºµÈ´Ù.
2.2.5 ÀÀ¿ëÇÁ·Î±×·¥ ÀúÀåÇϱâ¿Í ÄÄÆÄÀÏ, ½ÇÇà.
Java programÀº ASCII Çü½ÄÀÇ ÈÀÏ·Î ÀúÀåÇÏ¸é µÈ´Ù. È®ÀåÀÚ´Â *.java·Î ÇÑ´Ù.
Java compiler´Â Java source code¸¦ Java virgual machineÀ» À§ÇÑ
machine-levelÀÇ Java bytecode·Î ¹Ù²Ù¾îÁØ´Ù.
Java bytecode´Â Java interpreter¿¡ ÀÇÇؼ ½ÇÇà µÈ´Ù.
Java source fileÀ» compilerÇϸé È®ÀåÀÚ°¡ *.classÀÇ ÈÀÏÀ» »ý¼ºÇÑ´Ù.
*.classÀÇ ÈÀÏ À̸§Àº source code¿¡ ¼±¾ðµÇ¾î ÀÖ´Â classÀÇ À̸§À» ÀÌ¿ëÇÏ¿©
classÀÇ °¹¼ö¸¸Å *.classÈÀÏÀÌ »ý¼ºµÈ´Ù.
Java interpreter·Î main() method°¡ ÀÖ´Â *.classÈÀÏÀ» ½ÇÇà ½ÃŲ´Ù.
2.3 Java¾ð¾îÀÇ ±¸¼º¿ä¼Òµé.
´ÙÀ½ÀÇ °£´ÜÇÑ Java applicationÀ» ÇÑÁÙ¾¿ º¸¸é¼, Java languageÀÇ
syntax¿Í semantics¿¡ ´ëÇؼ ¾Ë¾Æº¸°Ú´Ù. ¶ÇÇÑ ¸î°¡Áö Java programming
ȯ°æ¿¡ ´ëÇؼµµ ¾Ë¾Æº¸°Ú´Ù.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
À§ÇÁ·Î±×·¥Àº ÀÔ·ÂÀ» ¹Þ°í ±× Ƚ¼ö, Áï ¹®ÀÚÀÇ °¹¼ö¸¦ º¸¿©ÁÖ´Â ÇÁ·Î±×·¥ÀÌ´Ù.
(character counting program)
°á°ú´Â ´ÙÀ½°ú °°´Ù.
% java Count
This is a test.
Input has 16 chars.
°³Ç๮ÀÚ(Enter key)±îÁö 16¹øÀÇ ÀÔ·ÂÀ» ¹Þ¾Ò´Ù.
2.3.1 °£´ÜÇÑ Å¬·¹½º Á¤ÀÇÇϱâ.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
Java language¿¡¼, ¸ðµç method¿Í variableÀº class¾È¿¡ ÀÖ¾î¾ß¸¸ ÇÑ´Ù.
±×·¡¼, ÇÁ·Î±×·¥ÀÇ Ã¹ÁÙ¿¡ Count¶ó´Â class¸¦ Á¤ÀÇÇÏ°í ÀÖ´Ù. ±×¾È¿¡¼´Â
character counting application¿¡¼ ÇÊ¿äÇÑ method¿Í variableÀ» Á¤ÀÇÇÑ´Ù.
Count class¿¡¼´Â main() method¸¸À» Á¤ÀÇÇÏ°í ÀÖ´Ù.
2.3.2 'main()' ¸Þ¼Òµå.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
Java language¿¡¼ Java interpreter·Î class¸¦ ½ÇÇà ½Ãų¶§, runtime systemÀº
classÀÇ main() method¸¦ È£ÃâÇÏ¸é¼ ½ÃÀÛÇÑ´Ù. ±×·¯¸é, main() method´Â
applicationÀ» ½ÇÇà½ÃÅ°´Âµ¥ ÇÊ¿äÇÑ ´Ù¸¥ method¸¦ ½ÇÇà ½ÃŲ´Ù.
2.3.3 ¿¹¿Ü»óȲÀÇ ¼Ò°³.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
´ÙÀ½¿¡³ª¿À´Â ¹®ÀåÀº 7À» 0À¸·Î ³ª´©´Â ¼öÇÐÀûÀÎ ¿À·ù¸¦ ¹üÇÏ°í ÀÖ´Ù.
int x = 0;
int y = 7;
System.out.println("answer = " + y/x);
ÇÁ·Î±×·¥À» ½ÇÇà Çϴµ¿¾È¿¡ À§¿Í °°Àº ¿À·ù°¡ ¹ß»ýµÉ¶§, Áï
´õÀÌ»ó ÇÁ·Î±×·¥À» ÁøÇà ÇÒ¼ö ¾ø´Â »óȲÀÌ ¹ß»ý µÉ¶§¸¦ ¿¹¿Ü»óȲ(exception)
À̶ó°í ÇÑ´Ù.
¿©·¯°¡Áö computer systemÀÇ ¿¹¿Ü»óȲ ó¸®(handle exception)´Â
°¢°¢ÀÇ ¼·Î ´Ù¸¥ ¹æ¹ýÀ¸·Î ó¸®ÇÏ°íÀÖ´Ù.
Java language¿¡¼´Â exception handler¸¦ ÀÌ¿ëÇÏ¿©, ¿¹¿Ü »óȲÀ»
catch, tryÇÒ ¼ö ÀÖ´Ù.
Exception handler´Â ¿À·ù¸¦ ¹æÁöÇϱâ À§Çؼ tryÇÒ ¼ö ÀÖ°í,
¸¸¾à µ¹ÀÌų ¼ö ¾ø´Â ¿À·ù°¡ ¹ß»ýÇϸé exception handler´Â
»ç¿ëÀÚ°¡ ¹®Á¦Á¡À» ÇØ°á ÇÒ ¼ö ÀÖµµ·Ï ¿À·ù ¸Þ½ÃÁö¸¦ º¼ ¼ö ÀÖµµ·Ï ÇÑ´Ù.
Java language¿¡¼, ¸ðµç method´Â throwÇÒ ¼ö ÀÖµµ·Ï °¡´ÉÇÑÇÑ
¸ðµç exceptionÀ» ¼±¾ð ÇؾßÇÑ´Ù.
À§ÀÇ main() method¿¡¼´Â ¿¹¿Ü»óȲÀ» ó¸®Çϱâ À§Çؼ(throw an exception)
java.io.IOExceptionÀ» È£Ãâ ÇÑ´Ù.
2.3.4 º¯¼ö¿Í µ¥ÀÌŸÇü.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
À§ÀÇ ÇÁ·Î±×·¥¿¡¼´Â Áö¿ª º¯¼ö·Î count Çϳª¸¸ ¼±¾ð ÇÏ¿´´Ù.
ÀÔ·ÂÀ» ¹ÞÀ»¶§¸¶´Ù count°ªÀº Çϳª¾¿ Áõ°¡¸¦ ÇÏ°Ô µÈ´Ù.
Java language¿¡¼ Á¦°øµÇ´Â data typeÀº ´ÙÀ½°ú °°´Ù.
Type Size/Format
byte 8-bit two's complement
short 16-bit two's complement
int 32-bit two's complement
long 64-bit two's complement
float 32-bit IEEE 754 floating point
double 64-bit IEEE 754 floating point
char 16-bit Unicode character
ÇÁ·Î±×·¥¿¡¼ count´Â Á¤¼öÇüÀ¸·Î ¼±¾ðµÇ¾ú´Ù. Data type¿¡ µû¶ó¼
°ªÀÇ ÇÑ°èÁ¡°ú, ¿¬»êÀÚ¸¦ °áÁ¤ÇÏ°Ô µÈ´Ù. ¿¹¸¦ µé¸é
Á¤¼öÇüÀº ¾ç¼ö¿Í À½¼ö¸¦ °¡Áú ¼ö ÀÖ°í, +, -¿Í °°Àº ¿¬»êÀÚ¸¦ ÀÌ¿ëÇÏ¿©
´õÇϱâ, »©±â¸¦ ÇÒ ¼ö ÀÖ´Â °ÍÀÌ´Ù.
'= 0'Àº variableÀ» 0À¸·Î ÃʱâÈ ½ÃÅ°´Â °ÍÀÌ´Ù.
¸¸¾à º¯¼ö¸¦ ÃʱâÈ ½ÃÅ°Áö ¾Ê°í »ç¿ëÇÒ·Á°í Çϸé, compiler´Â
¿À·ù ¸Þ½ÃÁö¸¦ ³»º¸ÀÌ°Ô µÈ´Ù.
Java language´Â ¶ÇÇÑ array, strings, objects¿Í °°Àº ¿©·¯°¡Áö
complex data typeÀ» Á¦°øÇÑ´Ù.
À§¿Í°°Àº ÀÌ¹Ì Á¦°øµÈ data typeÀ» »ç¿ëÇÏ´Â°Í »Ó¸¸ ¾Æ´Ï¶ó
'class', 'interface'¸¦ ÀÌ¿ëÇÏ¿© ÀÚ½ÅÀÇ data typeÀ» Á¤ÀÇÇÏ¿© »ç¿ëÇÒ ¼ö ÀÖ´Ù.
¾Ë°íº¸¸é ÀÌ¹Ì Count¶ó´Â class¸¦ Á¤ÀÇÇÏ¿´Áö ¾Ê¾Ò´Â°¡?
2.3.5 È帧Á¦¾î ¹®Àå.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
character counting program¿¡¼ ÀÔ·ÂÀ» ¿¬¼ÓÀûÀ¸·Î ¹Þ±âÀ§ÇÑ loop¸¦
while ¹®À¸·Î Ç¥ÇöÇÏ¿´´Ù. while¹®ÀÇ Çü½ÄÀº ´ÙÀ½°ú °°´Ù.
while (expression)
statement
ÀÌ°ÍÀº expressionÀÌ ÂüÀÏ µ¿¾È, statement¸¦ ½ÇÇà ÇÑ´Ù´Â ÀǹÌÀÌ´Ù.
read() method°¡ ½ÇÇàµÇ¾î return µÇ´Â character°ªÀÌ -1ÀÌ ¾Æ´Ï¸é
count¸¦ Çϳª Áõ°¡ ½ÃŲ´Ù.
±×¹ÛÀÇ Á¦¾î¹®Àº ´ÙÀ½°ú °°´Ù.
Statement Keyword
decision making if-else, switch
loops for, while, do-while
exceptions try-catch-throw
miscellaneous break, continue, label:, return
Java language¸¦ goto¸¦ ¿¹¾à¾î·Î Á¤ÀÇÇÏ°í ÀÖÁö¸¸, goto¹®À» Á¦°øÇÏÁö´Â ¾Ê´Â´Ù.
2.3.6 Ç¥ÁØÀÔ·Â.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
SystemÀº java.lang packageÀÇ memberÀÌ°í, Ç¥ÁØ ÀԷ°ú Ãâ·Â, ¹è¿º¹»ç,
ÇöÀç ½Ã°£°ú ³¯Â¥¸¦ ¾ò´Â ¹æ¹ý, ȯ°æº¯¼ö¸¦ ¾ò¾î³»´Â°Í °°Àº system
functionÀ» Á¦°øÇÑ´Ù. À̶§ while loop°¡ »ó¿ëµÇ¾úµû.
System classÀÇ ¸ðµç method¿Í variableÀº class methodÀÌ°í class variableÀÌ´Ù.
System.inÀº Ç¥ÁØ ÀÔ·Â(standard input stream)À» ó¸®ÇÑ´Ù. Java languageÀÇ
Ç¥ÁØ ÀÔ·Â(standard input stream)Àº C libraryÀÇ °³³ä°ú ºñ½ÁÇÑ °ÍÀÌ´Ù.
StreamÀº characterÀÇ flowing buffer·Î½á, º¸Åë standard input streamÀº
keyboard·ÎºÎÅÍ character¸¦ ÀоîµéÀδÙ.
standard input streamÀº text-based application¿¡¼ »ç¿ëÀڷκÎÅÍ
ÀÔ·ÂÀ» ¹Þ´Â ±â´ÉÀ» ÇÑ´Ù.
read() method´Â System.in¿¡¼ Á¦°øµÇ¾î ÇϳªÀÇ character¸¦ Àоî
ÀÐÀº character¸¦ returnÇØÁØ´Ù. ¸¸¾à character°¡ ¾øÀ¸¸é -1À» return ÇÑ´Ù.
Program¿¡¼ standard input streamÀ¸·Î ºÎÅÍ °ªÀ» ÀÐÀ»¶§, programÀº
blockµÇ¾î »ç¿ëÀÚ°¡ °ªÀ» ÃijÖÀ» µ¿¾È ±â´Ù¸°´Ù. ÇÁ·Î±×·¥Àº ´ÙÀ½°ú °°Àº,
»ç¿ëÀÚ°¡ ÀÔ·ÂÀ» ¸¶ÃÆ´Ù´Â °ªÀ» ³ÖÀ»¶§±îÁö °è¼Ó ±â´Ù¸°´Ù.
- UNIX
-
control-D character ¸¦ »ç¿ëÇÑ´Ù.
control-D character ¹®ÀÚ´Â
^D
ó·³ ³ªÅ¸Åº´Ù..
% java Count
This is test.
^D
Input has 16 chars.
- DOS shell (Windows 95/NT)
-
control-Z character ¸¦ »ç¿ëÇÑ´Ù.
control-Z character ¹®ÀÚ´Â
^Z
ó·³ ³ªÅ¸³´Ù..
C:\> java Count
This is test.
^Z
Input has 17 chars.
ÁÖÀÇÇÒ Á¡Àº, UNIX¿¡¼´Â '\n'ÀÌÁö¸¸, DOS¿¡¼´Â '\r\n'ÀÌ´Ù.
2.3.7 ¿¬»êÀÚ.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
character counting program¿¡¼´Â [], =, !=, ++, + ¿Í°°Àº
¸î°¡Áö ¿¬»êÀÚ¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
´Ù¸¥ ÇÁ·Î±×·¡¹Ö ¾ð¾î¿Í ¸¶Âù°¡Áö·Î Java languageµµ arithmetic,
logical, relational, stringÀ̶ó´Â 4°³ÀÇ ¿¬»êÀÚ category¸¦ °¡Áö°í ÀÖ´Ù.
¸î°¡Áö ¿¹¿Ü¸¦ Á¦¿ÜÇÏ°í ´ëü·Î JavaÀÇ ¿¬»êÀÚ´Â ´ÙÀ½°ú °°ÀÌ
¿¬»êÀÚ°¡ ÇÇ¿¬»êÀÚ Áß°£¿¡ ¿À´Â infix notationÀ» »ç¿ëÇÏ°í ÀÖ´Ù.
op1 operator op2
Arithmetic Operators.
Java language´Â ´õÇϱâ, »©±â, °öÇϱâ, ³ª´©±âµî ¿©·¯°¡Áö ¼öÇÐÀû¿¬»êÀÚ¸¦
Á¦°øÇÏ°í ÀÖ´Ù.
count++´Â °ªÀ» Çϳª Áõ°¡½ÃÅ°´Â short cut operator ++¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
Relational Opertors.
µÎ°³ÀÇ °ªÀ» ºñ±³ÇÏ´Â ¿¬»êÀÚ´Ù.
!= ´Â µÎ°³ÀÇ °ªÀÌ °°Áö ¾ÊÀ»¶§ Âü°ªÀ» returnÇÑ´Ù.
Logical Operators.
µÎ°³ÀÇ °ªÀ» ºñ±³ÇÏ¿© boolean logic opertionÀ» ¼öÇàÇÏ´Â ¿¬»êÀÚ´Ù.
&&, ||´Â boolean and, boolean or¿¬»êÀÚÀÌ´Ù.
ÇÁ·Î±×·¡¸Ó´Â º¹ÇÕÀûÀÎ Á¶°ÇÀ» Ç¥ÇöÇÒ¶§ logical operator¸¦ »ç¿ëÇÑ´Ù.
´ÙÀ½Àº ¹è¿ÀÇ index°¡ µÎ ÇÑ°è°ª»çÀÌ¿¡ ÀÖµµ·Ï Á¶°ÇÀ» ³ªÅ¸³»´Â °ÍÀÌ´Ù.
if (0 < index && index < NUM_ENTRIES)
String Operators.
Java language´Â +¿¬»êÀÚ¸¦ ¹®ÀÚ¿±îÁö È®ÀåÇÏ¿© ¹®ÀÚ¿À» µÚ¿¡ ´ùºÙÀÌ´Â
±â´ÉÀ» ÇÑ´Ù.
´ÙÀ½Àº ¹®ÀÚ¿ "Imput has", countÀÇ °ª, ¹®ÀÚ¿ " chars."¸¦
¿¬°á ½ÃÅ°´Â ¹®ÀåÀÌ´Ù.
System.out.println("Input has " + count + " chars.");
¿¬»êÀÚ ¿ì¼±¼øÀ§.
Java language´Â ´ÙÀ½°ú °°Àº º¹ÇÕÀûÀÎ ¿¬»êÇ¥ÇöÀ» ÇÒ ¼ö ÀÖ´Ù.
x * y * z
À§ÀÇ ¿¹´Â ¿¬»ê ¼ø¼°¡ º°Àǹ̰¡ ¾ø´Ù.
±×·¯³ª ´ÙÀ½ÀÇ Ç¥ÇöÀº ¼ø¼¿¡ µû¶ó ¼·Î ´Ù¸¥ °ªÀÌ ³ªÅ¸³´Ù.
x * y / 100
µû¶ó¼ ÇÁ·Î±×·¡¸Ó´Â 'x * (y / 100)' ó·³ °ýÈ£¸¦ ½á¼ ¸í½Ã¸¦ ÇØ¾ß ÇÑ´Ù.
2.3.8 Ç¥ÁØÃâ·Â.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
System class´Â ÀÌ¹Ì standard input stream¿¡¼ ¾Ë¾Æº¸¾Ò´Ù.
character counting program¿¡¼´Â Ãâ·ÂÀ» À§Çؼ System class¸¦ ´Ù½Ã »ç¿ëÇÑ´Ù.
println()°ú print()ÀÇ Â÷ÀÌ´Â print()´Â '\n'¸¦ Ãâ·ÂÇÏÁö ¾ÊÀ¸¹Ç·Î
°³Çà ÇÏÁö ¾Ê´Â´Ù´Â Á¡ÀÌ´Ù.
System.inÀº Ç¥ÁØ Ãâ·Â(standard output stream)À» ó¸®ÇÑ´Ù. Java languageÀÇ
Ç¥ÁØ Ãâ·Â(standard output stream)Àº C libraryÀÇ °³³ä°ú ºñ½ÁÇÑ °ÍÀÌ´Ù.
StreamÀº characterÀÇ flowing buffer·Î½á, º¸Åë standard output streamÀº
°¡Áö°í ÀÖ´Â °ªÀ» ȸ鿡 Ãâ·ÂÇÑ´Ù.
standard input streamÀº text-based application¿¡¼ »ç¿ëÀڷκÎÅÍ
ÀÔ·ÂÀ» ¹Þ´Â ±â´ÉÀ» ÇÑ´Ù.
2.3.9 ¹®ÀÚÇü.
class Count {
public static void main(String args[])
throws java.io.IOException
{
int count = 0;
while (System.in.read() != -1)
count++;
System.out.println("Input has " + count + " chars.");
}
}
C/C++¿¡¼, stringÀº ´ÜÁö NULL·Î ³¡³ª´Â characterÀÇ ¹è¿À̾ú´Ù.
±×·¯³ª, Java language¿¡¼´Â String classÀÇ object-instanceÀÌ´Ù.
System°ú ¸¶Âù°¡Áö·Î, String class´Â java.lang packageÀÇ memberÀÌ´Ù.
character counting program¿¡¼ StringÀº µÎ¹ø ¾²À̴µ¥,
ù¹ø°´Â main() method¿¡¼ ¾²¿´´Ù.
String args[]
À§ code¿¡¼´Â args¶ó´Â À̸§À¸·Î String object ¹è¿À» ¼±¾ðÇÏ°í ÀÖ´Ù.
´ë°ýÈ£°¡ ºñ¾îÀִ°ÍÀº compilationtime¿¡ ¹è¿ÀÇ ±æÀ̸¦ ¾Ë¼ö ¾ø´Ù´Â ÀǹÌÀÌ´Ù.
Compiler´Â Å«µû¿ÈÇ¥·Î ½Î¿©Áø ¹®ÀÚ¿(literal string)À» ¸¸³¯¶§¸¶´Ù
String objectÀ» ÇÒ´çÇÑ´Ù.
µû¶ó¼, À§ ÇÁ·Î±×·¥¿¡¼ µÎ°³ÀÇ ¹®ÀÚ¿ "Input has", " chars."Àº
³»ºÎÀûÀ¸·Î String object·Î º¯È¯µÈ´Ù.
Java language¿¡¼´Â Æí¸®»ó + ¿¬»êÀÚ¸¦ ÀÌ¿ëÇÏ¿©
´ÙÀ½°ú °°ÀÌ ¹®ÀÚ¿À» ¿¬°á ½ÃŲ´Ù.
"Input has " + count + " chars."
"Input has "¿Í " chars."´Â StringÀ¸·Î ¿¬°á µÉ ¼ö ÀÖÁö¸¸,
Áß°£¿¡ ÀÖ´Â count´Â ½ÇÁ¦·Î´Â integerÁö¸¸, ±×°ªÀ» ¹®ÀÚ¿·Î º¯È¯ÇÏ¿©
ó¸®ÇÑ´Ù.
2.3.10 ÀÀ¿ëÇÁ·Î±×·¥ ½ÇÇà½ÃÅ°±â.
2.3 ùºÎºÐ ÂüÁ¶.
2.4 'String' Ŭ·¹½º°ú 'StringBuffer' Ŭ·¹½º.
String, StringBuffer class¸¦ ÀÌ¿ëÇÏ¿© character data¸¦ ¾î¶»°Ô ó¸®ÇÏ´ÂÁö
¾Ë¾Æº¸°Ú´Ù. ¶ÇÇÑ ³»ºÎÀûÀ¸·Î compiler°¡ ¾î¶»°Ô ó¸®ÇÏ´Â Áöµµ ¾Ë¾Æº¸°Ú´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
À§ÀÇ ¿¹¾î¼ reverseIt() method´Â String°ú StringBuffer class µÑ´Ù »ç¿ëÇÏ¿©
stringÀÇ characterÀÇ ¼ø¼¸¦ ¿ªÀ¸·Î ¹Ù²Û´Ù.
¸¸¾à wordÀÇ list¸¦ °¡Áö°í ÀÖ´Ù¸é reverseIt() method¸¦ ÀÌ¿ëÇÏ¿©
µ¡ºÙ¿©°¡¸ç sortingÇÏ´Â ÇÁ·Î±×·¥À» ±¸Çö ÇÒ ¼ö ÀÖ´Ù.
reverseIt() method´Â ¼ø¼¸¦ ¿ªÀ¸·Î ¹Ù²Ü String typeÀÇ soruce¶ó´Â
À̸§ÀÇ argument¸¦ ¹Þ¾ÆµéÀδÙ. dest¶ó´Â soruce¿Í ±æÀÌ°¡ °°Àº
StringBuffer¸¦ »ý¼ºÇÑÈÄ, ¿ªÀ¸·Î ¹Ù²Ù±â À§Çؼ loop¿¡ µé¾î°£´Ù.
¿©±â¿¡¼ °¡Àå Áß¿äÇÑ Á¡Àº ¼·ÎÀÇ À¯¿ëÇÑ method¸¦ »ç¿ëÇϱâ À§Çؼ
type¸¦ ¼·Î ¹Ù²Ù¾î°¡¸ç ó¸®ÇÑ´Ù´Â °ÍÀÌ´Ù.
2.4.1 ¿Ö ¹®ÀÚ¿À» À§ÇÑ µÎ°¡Áö Ŭ·¹½º¸¦ ¾²´Â°¡?
Java¿¡¼´Â constant stringÀ» À§Çؼ String class¸¦Á¦°øÇÏ°í,
stringÀ» Á¶ÀÛÇϱâ À§Çؼ StringBuffer class¸¦ Á¦°øÇÑ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
stringÀÇ °ªÀ» ¹Ù²Ù°í ½ÍÁö ¾ÊÀ»¶§ constant stringÀ» À§ÇÑ String class¸¦
»ç¿ëÇÑ´Ù. ¿¹¸¦ µé¾î¼ string data¸¦ method¿¡ Àü´ÞÇØÁÙ¶§, method°¡
stringÀ» ¾î¶°ÇÑ ¹æ¹ýÀ¸·Îµµ ¹Ù²ÙÁö ¾Ê±â¸¦ ¹Ù¶õ´Ù¸é, StringÀ» »ç¿ëÇØ¾ß ÇÑ´Ù.
ÀϹÝÀûÀ¸·Î, StringÀº method¿¡ °ªÀ» Àü´ÞÇÒ¶§, method¿¡¼ °ªÀ» returnÇØÁÙ¶§
»ç¿ëÇÑ´Ù. reverseIt() method¿¡¼´Â argument·Ö return°ªÀ¸·Î StringÀ» »ç¿ëÇÑ´Ù.
stringÀÇ character°ªÀ» ¾Ë°í ÀÖ°í ±× °ªÀ» ¹Ù²Ü ÇÊ¿ä°¡ ÀÖµû¸é
non-constant stringÀ» À§ÇÑ StringBuffer class¸¦ »ç¿ëÇÑ´Ù.
ÀϹÝÀûÀ¸·Î reverseIt() methodó·³ character data¸¦ »ý¼º, ÃʱâÈÇÒ¶§
StringBuffer¸¦ »ç¿ëÇÑ´Ù.
StringÀº constantÀ̱⠶§¹®¿¡ StringBufferº¸´Ù ÀÛ°í, °øÀ¯ÇÒ ¼ö ÀÖ´Ù.
2.4.2 'String'°ú 'StringBuffer'»ý¼ºÇϱâ.
StringBuffer dest = new StringBuffer(len);
StringBuffer¸¦ »õ·Î »ý¼ºÇÒ¶§ ¼±¾ð, ÃʱâÈ, instantiationÀÇ 3´Ü°è¸¦ °ÅÄ£´Ù.
ÀÌ°ÍÀº ´Ù¸¥¸ðµç object¸¦ »ý¼ºÇÒ¶§µµ ¸¶Âù°¡ÁöÀÌ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
dest´Â ÃʱâÈÇϱâÀ§ÇÑ constructor method¿¡¼ StringBufferÀÇ Å©±â¸¦
³ªÅ¸³»´Â integer °ªÀÌ ÇÊ¿äÇÏ´Ù.
StringBuffer(int length)
¸¸¾à Å©±â¸¦ Á¤ÇØÁÖÁö ¾ÊÀ¸¸é, ´ÙÀ½¹ø¿¡ Å©±â°¡ ÁÖ¾îÁú¶§±îÁö ³»¹ö·ÁµÐ´Ù.
±×·¯³ª, ´ÙÀ½¿¡ ´Ù½Ã ¸Þ¸ð¸®¸¦ ÇÒ´çÇÏÁö ¾Êµµ·Ï óÀ½¿¡ Å©±â¸¦ Á¤ÇØÁÖ´Â
°ÍÀÌ ´õ ÁÁ´Ù.
2.4.3 Á¢±Ù ¸Þ¼Òµå.
reverseIt() method´Â sourceÀÇ Á¤º¸¸¦ °¡Á®¿À±â À§Çؼ 2°³ÀÇ
charAt()°ú length() accessor method¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
String°ú StringBuffer´Â substringÀ» ã°Å³ª, ƯÁ¤ characterÀÇ À§Ä¡¸¦ ¾Ë¾Æ³»´Â
¸î°¡Áö accessor method¸¦ Á¦°øÇÑ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
ObjectÀÇ instance variableÀº ¿ÜºÎÀÇ ´Ù¸¥ object·Î ºÎÅÍ °ªÀ» º¯°æÇÏÁö
¸øÇϵµ·Ï, objectÀÇ ³»ºÎ¿¡ °¡·ÁÁ® ÀÖ´Ù. ObjectÀÇ instance´Â, ¿ÀÁ÷ Àß ÀÛ¼ºµÈ
exceptionÀ» °¡Áö°í ÀÖ´Â objectÀÇ method¸¦ ÅëÇؼ¸¸ °ªÀ» Á¢±Ù, º¯Çü ÇÒ ¼ö
ÀÖ´Ù. ObjectÀÇ data¸¦ ĸ½¶È ÇÔÀ¸·Î½á ¿ÜºÎ·ÎºÎÅÍÀÇ À߸øµÈ »óȲÀ»
¹æÁöÇϱâÀ§ÇØ ³»ºÎÀûÀÎ Á¤º¸¸¦ ¿ÜºÎ·ÎºÎÅÍ ¼û±ä´Ù. ĸ½¶È´Â °´Ã¼ÁöÇâ
ÇÁ·Î±×·¡¹Ö ±â¹ýÀÇ ±âº»ÀÌ´Ù.
Object·ÎºÎÅÍ Á¤º¸¸¦ ¾ò¾î³»±â À§ÇÑ method¸¦ accessor method¶ó°í ÇÑ´Ù.
reverseit() method´Â soruce stirngÀÇ Á¤º¸¸¦ °¡Á®¿À±â À§Çؼ µÎ°³ÀÇ
StringÀÇ accessor method¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
ù¹ø° StringÀÇ length() accesor method´Â String sourceÀÇ ±æÀ̸¦
¾Ë¾Æ³»´Â ±â´ÉÀ» ÇÑ´Ù.
int len = source.length();
µÎ¹ø°, charAt() accessor method´Â parameter·Î ³Ñ°Ü¿Â À§Ä¡ÀÇ charcterÀÇ
°ªÀ» returnÇØÁØ´Ù.
source.charAt(i)
2.4.3.1 ´Ù¸¥ Á¢±Ù ¸Þ¼Òµå.
length(), charAt() ÀÌ¿Ü¿¡ StringÀº ¸î°¡Áö ´Ù¸¥ accessor method¸¦
Á¦°øÇÑ´Ù. StringBufferµµ ¸¶Âù°¡Áö·Î ºñ½ÁÇÑ accessor method¸¦ Á¦°øÇÑ´Ù.
StringÀº ƯÁ¤ ¹®ÀÚ¿ÀÇ character¸¦ ã¾Æ index¸¦ ³Ñ°ÜÁÖ´Â
indexOf(), lastIndexOf()¸¦ Á¦°øÇÑ´Ù. indexOf()´Â ¹®ÀÚ¿ÀÇ Ã³À½ ¾ÕºÎºÐ¿¡¼
ºÎÅÍ Ã£°í, lastIndexOf()´Â µÚ¿¡¼ ºÎÅÍ Ã£´Â´Ù.
indexOf(), lastIndexOf() method´Â stringÀÇ substringÀ» ã¾ÆÁÖ´Â
substring()°úÇÔ²² ¹®ÀÚ¿À» ¼·Î ¿¬°áÇÒ¶§ ¸¹ÀÌ ¾²ÀδÙ.
´ÙÀ½ÀÇ class´Â lastIndexOf()¿Í substring()À» »ç¿ëÇÏ¿©
filenameÀÇ °¢°¢ÀÇ ¼·Î´Ù¸¥ ºÎºÐÀ» ¶¼¾î³»´Â ±â´ÉÀ» ¼öÇàÇÑ´Ù.
´ÙÀ½ method´Â error checkingÀ» ÇÏÁö ¾Ê°í, argument´Â
full directory path¿Í filename°ú extentionÀ» °¡Áö°í Àִ°ÍÀ¸·Î °¡Á¤ÇÑ´Ù.
class Filename {
String fullpath;
char pathseparator;
Filename(String str, char sep) {
fullpath = str;
pathseparator = sep;
}
String extension() {
int dot = fullpath.lastIndexOf('.');
return fullpath.substring(dot + 1);
}
String filename() {
int dot = fullpath.lastIndexOf('.');
int sep = fullpath.lastIndexOf(pathseparator);
return fullpath.substring(sep + 1, dot);
}
String path() {
int sep = fullpath.lastIndexOf(pathseparator);
return fullpath.substring(0, sep);
}
}
extention() method´Â lastIndexOf()¸¦ »ç¿ëÇÏ¿© µÚ¿¡¼ ºÎÅÍ '.'À» ã¾Æ³»¾î
È®ÀåÀÚÀÇ ½ÃÀÛ ºÎºÐÀ» ã¾Æ³½ ÈÄ, substring()À» ÀÌ¿ëÇÏ¿© ÈÀÏÀÇ
È®ÀåÀÚ¸¦ ¾Ë¾Æ³½´Ù.
À§ ÇÁ·Î±×·¥Àº '.'À» Æ÷ÇÔÇÑ È®ÀåÀÚ¸¦ °¡Áö°í ÀÖ´Â filenameÀÌ argument·Î
³Ñ°Ü¿Â´Ù´Â °ÍÀ» °¡Á¤ÇÏ°í ÀÖÀ¸¸ç, ¸¸¾à '.'ÀÌ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é
lastIndexOf()´Â -1À» return ÇÏ°Ô µÇ°í, substring() method´Â ¿À·ù»óȲ¿¡¼
"string index out of range exception"À» ¼öÇàÇÏ°Ô µÈ´Ù.
À§ÀÇ ¿¹Á¦¾î¼´Â ÇÑ°¡Áö versionÀÇ indexOf()¿Í lastIndexOf() method¸¦
»ç¿ëÇÏ°í ÀÖÁö¸¸, ±×¹ÛÀÇ ´Ù¾çÇÑ ±â´ÉÀ» ¼öÇàÇÏ´Â ´Ù¸¥ versionÀÇ
indexOf()¿Í lastIndexOf() method°¡ ÀÖ´Ù.
-
returns the index of the first (last) occurrence of the specified character
-
returns the index of the first (last) occurrence of the specified character,
searching forward (backward) from the specified index
-
returns the index of the first (last) occurrence of the specified String.
-
returns the index of the first (last) occurrence of the specified String,
searching forward (backward) from the specified index
String°ú ¸¶Âù°¡Áö·Î, StringBufferµµ length()¿Í charAt() accessor method¸¦
Á¦°øÇÑ´Ù. ¶ÇÇÑ, capacity()¶ó´Â method¸¦ Á¦°øÇÑ´Ù.
capacity() method´Â ¹®ÀÚ¿ÀÇ ±æÀ̸¦ ¾Ë·ÁÁÖ´Â length()¿Í´Â ´Þ¸®,
StringBuffer¸¦ À§Çؼ ÇÒ´çµÈ ÇöÀç ¸Þ¸ð¸® Å©±â¸¦ return ÇØÁØ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
À§ÀÇ ÇÁ·Î±×·¥¿¡¼ ¹Ù²ï°Í ¾ø´Ù.
Loop¸¦ µ¹¸é¼ StringBufferÀÇ Å©±â´Â Áõ°¡µÈ´Ù.
2.4.4 'StringBuffer' º¯°æÇϱâ.
reverseIt() method¿¡¼ StringBufferÀÇ append() method´Â dest¿¡
character¸¦ µ¡ºÙÈù´Ù. StringBuffer´Â ±×¹Û¿¡ ƯÁ¤ À§Ä¡¿¡
character¸¦ buffer¿¡ ÷°¡ÇÏ´Â ±â´ÉÀ» °¡Áø methodµµ Á¦°øÇÑ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
append() method¿¡¼ ÇöÀçÀÇ bufferÅ©±âº¸´Ù ´õÅ« Å©±â¸¦ ¿ä±¸µÇ¸é,
¸Þ¸ð¸®¸¦ ´Ù½Ã ÇÒ´ç¹Þ¾Æ StringBufferÀÇ Å©±â¸¦ º¯°æÇÑ´Ù.
¸Þ¸ð¸® ÇÒ´çÀº ¸¹Àº ³ë·ÂÀ» ÇÊ¿ä·Î ÇÏ´Â ÀÛ¾÷À̱⠶§¹®¿¡, óÀ½¿¡ StringBufferÀÇ
Å©±â¸¦ ÃæºÐÈ÷ Àû´çÇÑ ¸¹Å ÇÒ´çÇÏ¿© ¸Þ¸ð¸®¸¦ ÇÒ´çÇϴ Ƚ¼ö¸¦ ÁÙÀÓÀ¸·Î½á
ÇÁ·Î±×·¥ÀÇ È¿À²À» ³ôÈú ¼ö ÀÖ´Ù.
À§ÀÇ ¿¹¾î¼ ó·³ destÀÇ Å©±â¸¦ soruceÀÇ Å©±â¿Í °°°Ô ÇÔÀ¸·Î½á
¸Þ¸ð¸® ÇÒ´çÀÇ È½¼ö¸¦ 1¹ø¸¸À¸·Î ¸¸Á·ÇÏ´Ù.
À§ÀÇ append()´Â StringBufferÀÇ ¸Ç ³¡ºÎºÐ¿¡ characer¸¦ ÷°¡ÇÑ´Ù.
±×¹Û¿¡ float, int, boolean°ú °°Àº ¿©·¯°¡Áö typeÀÇ data¸¦ ÷°¡ ÇÒ ¼öµµ ÀÖ´Ù.
À̶§ ÀÌ·¯ÇÑ data typeÀº stringÀÇ ÇüÅ·Πtype conversionÀÌ·ç¾îÁøÈÄ
µÚ¿¡ ´ùºÙÈ÷°Ô µÈ´Ù.
±×¹Û¿¡ StringBufferÀÇ Áß°£¿¡ data¸¦ »ðÀÔÇÏ°í ½ÍÀ» °æ¿ì°¡ ÀÖ´Ù.
À̶§ StringBufferÀÇ insert() method¸¦ »ç¿ëÇÑ´Ù.
´ÙÀ½Àº StringBufferÀÇ Áß°£¿¡ stringÀ» »ðÀÔÇÏ´Â ¿¹ÀÌ´Ù.
StringBuffer sb = new StringBuffer("Drink Java!");
sb.insert(6, "Hot ");
System.out.println(sb.toString());
À§ ÇÁ·Î±×·¥ÀÇ °£´ÜÇÑ °á°ú´Â ´ÙÀ½°ú °°´Ù.
Drink Hot Java!
À§ÀÇ ¿¹¿¡¼ º¼ ¼ö ÀÖµíÀÌ, insert()¸¦ »ç¿ëÇϱâ À§Çؼ´Â, index¸¦ ¸í½ÃÇؾß
ÇÑ´Ù. "Hot "ÀÌ µé¾î°¥ ÀÚ¸®´Â "Java"ÀÇ 'J'°¡ ÀÖ´Â ÀÚ¸®ÀÌ´Ù.
µû¶ó¼, stringÀÇ index´Â 0¿¡¼ ºÎÅÍ ½ÃÀÛÇÏ°í 'J'ÀÇ À§Ä¡´Â 6ÀÌ µÈ´Ù.
¸¶Âù°¡Áö·Î, append()¿¡¼ ¸Ç µÚ¿¡ character¸¦ ´ùºÙÈù´Ù´Â°ÍÀº
¹®ÀÚ¿ÀÇ ±æÀ̸¦ index·Î ÇÏ¿© ±×°÷¿¡ ´ùºÙÈù´Ù´Â°Í°ú °°Àº ÀǹÌÀÌ´Ù.
±×¹Û¿¡ StringBufferÀÇ Æ¯Á¤ À§Ä¡¿¡ charcter°ªÀ» º¯°æÇÏ´Â setCharAt()À»
Á¦°øÇÑ´Ù.
2.4.5 °´Ã¼¸¦ ¹®ÀÚ¿·Î º¯È¯.
reverseIt()¿¡¼ StringBuffer´Â StringÀ¸·Î º¯°æµÇ¾î returnµÈ´Ù.
ÀÌ·¸°Ô, StringÀÇ vlaueOf() method¸¦ ÀÌ¿ëÇÏ¿© ¿©·¯°¡Áö ´Ù¸¥ typeÀº
StringÀ¸·Î º¯°æµÉ ¼öÀÖ´Ù.
class ReverseString {
public static String reverseIt(String source) {
int i, len = source.length();
StringBuffer dest = new StringBuffer(len);
for (i = (len - 1); i >= 0; i--) {
dest.append(source.charAt(i));
}
return dest.toString();
}
}
Á¾Á¾ object´Â method°¡ ¿ÀÁ÷ String °ªÀ» ó¸®ÇÒ ¼ö ÀÖµµ·Ï
StringÀ¸·Î º¯È¯ÇÒ ÇÊ¿ä°¡ ÀÖ´Ù.
¿¹¸¦ µé¾î¼ System.out.printIn()Àº StringBuffer¸¦ Çã¿ëÇÏÁö ¾ÊÀ¸¹Ç·Î,
StringBuffer´Â StringÀ¸·Î º¯È¯ÇÒ ÇÊ¿ä°¡ ÀÖ´Ù.
À§ÀÇ ¿¹¿¡¼ reverseIt() method´Â returnÇÒ ¶§ StringBuffer¸¦ StringÀ¸·Î
º¯È¯Çϱâ À§Çؼ StringBufferÀÇ toString() method¸¦ »ç¿ëÇÏ¿´´Ù.
return dest.toString();
java.langÀÇ ¸î°³ÀÇ class¿¡¼´Â Character, Integer, Boolean°ú °°Àº
"type wripper" class¸¦ Æ÷ÇÔÇÑ toString()À» Á¦°øÇÑ´Ù.
Object class´Â Object¸¦ StringÀ¸·Î º¯È¯Çϱâ À§Çؼ toString()À» Á¦°øÇÑ´Ù.
ObjectÀÇ subclass¸¦ ÀÛ¼ºÇÒ¶§, toString()Àº subclass¿¡¼ ´Ù¸¥ ÇüÅÂÀÇ ÀÏÀ»
ÇÏ´Â method·Î ´Ù½Ã Á¤ÀÇ(override) ÇÒ ¼ö ÀÖ´Ù.
¶ÇÇÑ ÆíÀÇ»ó, String class´Â ´Ù¸¥ typeÀÇ variableÀ» StringÀ¸·Î ¹Ù²Ù¾îÁÖ´Â
static method·Î valueOf()¸¦ Á¦°øÇÑ´Ù.
¿¹¸¦ µé¾î¼, ´ÙÀ½Àº pi ÀÇ °ªÀ» ÇÁ¸°Æ®ÇØÁØ´Ù.
System.out.println(String.valueOf(Math.PI));
2.4.6 °´Ã¼¸¦ ¼ýÀÚ·Î º¯È¯.
Integer, Float, Double, Long class´Â ±× ¼ýÀÚ°ªÀ» StringÀ¸·Î º¯È¯ ÇÒ ¼ö ÀÖ´Ù.
String classÀÚü ¿¡¼´Â StringÀ» ½Ç¼ö³ª Á¤¼ö ¶Ç´Â ´Ù¸¥ ÇüÅÂÀÇ ¼ýÀÚ·Î º¯È¯
ÇØÁÖ´Â method¸¦ Á¦°øÇÏÁö ¾Ê´Â´Ù.
±×·¯³ª, 4°³ÀÇ "type wrapper" class(Integer, Double, Float, Long)´Â
StringÀ» °¢°¢ÀÇ ¼ýÀÚ °ªÀ¸·Î ¹Ù²Ù¾îÁÖ´Â valueOf()¶ó´Â method¸¦ Á¦°øÇÑ´Ù.
´ÙÀ½Àº Float classÀÇ valueOf() methodÀÇ ¿¹¸¦ º¸¿©ÁÖ°í ÀÖ´Ù.
String piStr = "3.14159";
Float pi = Float.valueOf(piStr);
2.4.7 ¹®ÀÚ¿°ú Java ÄÄÆÄÀÏ·¯.
String°ú StringBuffer´Â ±×¹ÛÀÇ ´ùºÙÈ÷±â, À߶󳻱â, ±³È¯Çϱâ, ºñ±³Çϱâ,
´ë/¼Ò¹®ÀÚ·Î º¯È¯Çϱ⠵î string data¸¦ ó¸®Çϱâ À§ÇÑ ¸¹°íµµ À¯¿ëÇÑ
method¸¦ Á¦°øÇÏ°í ÀÖ´Ù.
¸¶Áö¸·À¸·Î, Java compiler°¡ literal stringÀ» ³»ºÎÀûÀ¸·Î ¾î¶»°Ô
String, StringBuffer·Î º¯È¯ÇÏ´ÂÁö ¾Ë¾Æº»´Ù.
Java language¿¡¼ ´ÙÀ½Ã³·³ C/C++¿¡¼ ¾²´Â°Í°ú ¸¶Âù°¡Áö·Î Å«µû¿ÈÇ¥·Î
µÑ·¯½ÎÀÎ liternal ¹®ÀÚ¿À» ÀÛ¼ºÇÑ´Ù.
"Hello World!"
literal stringÀº StringÀÌ µé¾î°¥ ¼ö Àִ°÷Àº ¾îµð¿¡³ª ¾µ ¼ö ÀÖ´Ù.
´ÙÀ½Àº System.out.println()ÀÌ String´ë½Å literal stringÀ» »ç¿ëÇÏ´Â
¿¹ÀÌ´Ù.
System.out.println("And might I add that you look lovely today.");
¶ÇÇÑ literal string¿¡ ¹Ù·Î String method¸¦ »ç¿ëÇÒ ¼ö µµ ÀÖ´Ù.
int len = "Goodbye Cruel World".length();
compiler´Â literal stringÀ» ¸¶ÁÖÄ¥¶§ ¸¶´Ù ÀÚµ¿À¸·Î String object¸¦
»õ·Ó°Ô »ý¼ºÇϱ⠶§¹®¿¡, literal stringÀº StringÀ» ÃʱâÈ ½ÃÅ°´Âµ¥µµ ¾²ÀδÙ.
String s = "Hola Mundo";
String s = new String("Hola Mundo");
À§ÀÇ °Í°ú ¾Æ·¡ÀÇ °Í°ú °°Áö¸¸, À§ÀÇ°ÍÀÌ ´õ¿í È¿À²ÀûÀÌ´Ù.
¿Ö³ªÇϸé, compiler´Â "Hello World"°¡ ³ªÅ¸³¯¶§ Çѹø,
new String()ÀÌ ³ªÅ¸³¯ ¶§ Çѹø, µÎ¹ø StringÀ» »ý¼ºÇϱ⠶§¹®ÀÌ´Ù.
Java language¿¡¼, StringÀ» ¿¬°á ÇÒ¶§ '+'¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.
String cat = "cat";
System.out.println("con" + cat + "enation");
À§ÀÇ °ÍÀº º¸±âÁÁ°í Çö¶õÇϱ⠱îÁö Çѵ¥, StringÀº º¯°æÇÒ ¼ö ¾ø´Â objectÀÌ´Ù.
µû¶ó¼, compiler´Â ³»ºÎÀûÀ¸·Î ´ÙÀ½°ú °°Àº code·Î º¯È¯ÇÏ¿© ½ÇÇà ÇÑ´Ù.
String cat = "cat";
System.out.println(new StringBuffer.append("con").append(cat).append("enation"));
2.5 ¸í·É¾î Argument.
Java application¿¡¼ command line argument¸¦ ¾î¶»°Ô ó¸®ÇÏ´ÂÁö ¾Ë¾Æº»´Ù.
Java interperter´Â ´ÜÁö command line argumnet¸¦ java application¿¡ Àü´Þ
ÇØÁÙ »ÓÀÌ´Ù. applet¿¡¼´Â ´ë½Å parameter¸¦ »ç¿ëÇÑ´Ù.
Java applicationÀº¿¡¼´Â command lineÀ¸·ÎºÎÅÍ ¿©·¯°³ÀÇ argument¸¦
ÀüÇØ¹Þ¾Æ Ã³¸®ÇÒ ¼ö ÀÖ´Ù. Command line argument´Â »ç¿ëÀڷκÎÅÍ
applicationÀÇ ¿¬»êÀÇ ¹æ¹ý¿¡ ¿µÇâÀ» ÁÙ ¼ö ÀÖ´Ù.
ApplicationÀ» ½ÇÇà ½Ãų¶§, application name ´ÙÀ½¿¡ command line argument¸¦
ÀÔ·Â ÇÒ ¼ö ÀÖ´Ù. ¿¹¸¦ µé¾î¼, Sort¶ó´Â applicationÀ» °¡Áö°í ÀÖ´Ù°í ÇÒ¶§,
fileÀÇ sortingÇÒ·Á´Â file À̸§À» ÁöÁ¤ÇÒ ¼ö ÀÖ´Ù.
C:\> java Sort ListOfFriends
´ÙÀ½ ÇÁ·Î±×·¥Àº command line argument¸¦ ¹Þ¾Æ¼ Çϳª¾¿ Ãâ·ÂÇÏ´Â ¿¹Á¦ÀÌ´Ù.
class Echo {
public static void main (String args[]) {
for (int i = 0; i < args.length; i++)
System.out.println(args[i]);
}
}
DOS prompt»ó¿¡¼ ´ÙÀ½°ú °°ÀÌ ½ÇÇà ½Ãų ¼ö ÀÖ´Ù.
C:\> java Echo Drink Hot Java
Drink
Hot
Java
2.5.1 °ø¹éºÐÀÚ´Â ¸í·É¾î Argument¸¦ ±¸ºÐÇÑ´Ù.
ÇÏÁö¸¸ ´ÙÀ½°ú °°ÀÌ ÀÔ·ÂÇϸé space¸¦ ¹«½ÃÇÏ°Ô µÇ¹Ç·Î argument´Â 1°³ÀÎ ¼ÀÀÌ´Ù.
% java Echo "Drink Hot Java"
Drink Hot Java
2.5.2 Æí¸®¼º.
Unix¿¡´Â word arguments, arguments that require arguments, flags
3°¡Áö Á¾·ùÀÇ command line argument°¡ ÀÖ´Ù.
±× 3°¡Áö argument¸¦ ´ÙÀ½ÀÇ ¿¹¿¡¼ ó·³ Java¿¡¼ ±¸ÇöÇÒ ¼ö ÀÖ´Ù.
´ÙÀ½Àº ¾Æ·¡¿Í °°Àº »ç¿ë¹ýÀ» °¡Áö´Â ÇÁ·Î±×·¥ÀÌ´Ù.
usage: application_name [ optional_args ] required_args
´ÙÀ½Àº word argument¸¦ ó¸®ÇÏ´Â ¿¹ÀÌ´Ù.
if (argument.equals("-verbose"))
vflag = true;
´ÙÀ½Àº Argument¸¦ ÇÊ¿ä·Î ÇÏ´Â Argument¸¦ ó¸®ÇÏ´Â ¹æ¹ýÀÌ´Ù.
Áï, Argument ´ÙÀ½¿¡ ¶Ç´Ù¸¥ Argument¸¦ ²À ÇÊ¿ä·Î ÇѴ°ÍÀÌ´Ù.
if (argument.equals("-output")) {
if (nextarg < args.length)
outputfile = args[nextarg++];
else
System.err.println("-output requires a filename");
}
´ÙÀ½À» command line¿¡¼ Flag¸¦ ÁöÁ¤ÇÏ´Â Çü½Ä°ú, Flag¸¦ ó¸®ÇÏ´Â ¹æ¹ýÀÌ´Ù.
-x -n or -n -x
À§ÀÇ °ÍÀº »ç¿ëÀÚÀÇ Æí¸®¼ºÀ» À§Çؼ ´ÙÀ½°ú °°Àº Çü½Äµµ Çã¿ëÇϸé ÁÁ´Ù.
-nx or -xn
ÀÌ°ÍÀ» ó¸®Çϱâ À§ÇÑ ¿¹ÀÌ´Ù.
for (j = 1; j < arg.length(); j++) {
flag = arg.charAt(j);
switch (flag) {
case 'x':
if (vflag) System.out.println("Option x");
break;
case 'n':
if (vflag) System.out.println("Option n");
break;
default:
System.err.println("ParseCmdLine: illegal option " + flag);
break;
}
}
2.5.3 ¸í·É¾î Argument ó¸®.
class ParseCmdLine {
public static void main(String args[]) {
int i = 0, j;
String arg;
char flag;
boolean vflag = false;
String outputfile = "";
while (i < args.length && args[i].startsWith("-")) {
arg = args[i++];
// word argument
// use this type of check for "wordy" arguments
if (arg.equals("-verbose")) {
System.out.println("verbose mode on");
vflag = true;
}
// use this type of check for arguments that require arguments
else if (arg.equals("-output")) {
if (i < args.length)
outputfile = args[i++];
else
System.err.println("-output requires a filename");
if (vflag)
System.out.println("output file = " + outputfile);
}
// use this type of check for a series of flag arguments
else {
for (j = 1; j < arg.length(); j++) {
flag = arg.charAt(j);
switch (flag) {
case 'x':
if (vflag) System.out.println("Option x");
break;
case 'n':
if (vflag) System.out.println("Option n");
break;
default:
System.err.println("ParseCmdLine: illegal option " + flag);
break;
}
}
}
}
if (i == args.length)
System.err.println("Usage: ParseCmdLine [-verbose] [-xn] [-output afile] filename");
else
System.out.println("Success!");
}
}
JavaÀÇ command line argument´Â C/C++°ú´Â ´Ù¸¥´Ù.
C/C++Àº argc¿Í argv¸¦ ³Ñ°ÜÁÖÁö¸¸, Java´Â argsÇϳª¸¸À» ³Ñ°ÜÁØ´Ù.
´ÙÀ½Àº C/C++ÀÇ argumentÇü½Ä°ú JavaÀÇ argumentÇü½Ä°ú ºñ±³ÇÏ¿´´Ù.
diff file1 file2
C/C++ argvÀÇ Ã¹¹ø° command line argumentsms diffÀÌ´Ù.
java diff file1 file2
JavaÀÇ Ã¹¹ø° command line argumentsms file1ÀÌ´Ù.
Runtime systemÀº Command line argumentÀ» ¿ÀÁ÷ Java application¿¡¸¸
³Ñ°ÜÁØ´Ù. ´ë½Å Java appletÀº parameter¸¦ »ç¿ëÇϴµ¥ ±×Çü½ÄÀº ´Ù¸£´Ù.
2.6 Thread ó¸®.
À̹ø¿¡´Â thread¸¦ ÀÌ¿ëÇÏ¿© µ¿½Ã¿¡ ¿©·¯°¡Áö ÀÏÀ» ¼öÇà ÇÏ´Â Java application
¶Ç´Â appletÀ» »ìÆ캸°Ú´Ù.
¿Ö? ¶Ç´Â ¾ðÁ¦ thread¸¦ »ç¿ëÇϴ°¡?
Thread¿Í thread groupÀ» ¾î¶»°Ô ó¸®ÇÏ°í, deadlockÀ̳ª race conditionÀ»
¾î¶»°Ô ÇÇÇÏ´ÂÁö¿¡ ´ëÇؼ ¾Ë¾Æº»´Ù.
2.6.1 Thread¶õ ¹«¾ùÀΰ¡?
Thread´Â ÀϹÝÀûÀ¸·Î process¿¡ ÀÇÇؼ controlµÇ´Â execution context ¶Ç´Â
lightweight process¶ó°í ºÎ¸¥´Ù.
¸ðµç ÇÁ·Î±×·¡¸Ó´Â ¼øÂ÷ÀûÀÎ ÇÁ·Î±×·¥¿¡ Àͼ÷ÇØÀÖ´Ù.
¾Æ¸¶ ´ç½ÅÀº "Hello World!"¶ó°í Ãâ·ÂÇØÁÖ´Â ÇÁ·Î±×·¥À̳ª, À̸§, ¹øÈ£¸¦
sortÇØÁÖ´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇغ» °æÇèÀÌ ÀÖÀ»°ÍÀÌ´Ù.
±×·¯³ª ÀÌ·¸ ÇÁ·Î±×·¥Àº ½ÃÀÛÇؼ Çϳª¾¿ ¼öÇàÇسª°¡¸ç ³¡À» º¸´Â
¼øÂ÷ÀûÀÎ ÇÁ·Î±×·¥ÀÌ´Ù.
Threadµµ ¼øÂ÷ÀûÀÎ ÇÁ·Î±×·¥°ú À¯»çÇÏ´Ù.
ÇϳªÀÇ threadÇÁ·Î±×·¥ ¶ÇÇÑ ½ÃÀÛ°ú ³¡±îÁö Çϳª¾¿ ¼öÇàÇسª°¡´Â ¼øÂ÷ÀûÀÎ
ÇÁ·Î±×·¥°ú °°´Ù.
±×·¯³ª, thread ÀÚü´Â ÇÁ·Î±×·¥Àº ¾Æ´Ï´Ù. Áï, È¥ÀÚ ¼öÇàÇÒ ¼ö ÀÖ´Â ´ÜÀ§°¡
¾Æ´Ï¶ó ÇÁ·Î±×·¥ ¾È¿¡¼ ¼öÇàµÇ´Â °ÍÀÌ´Ù.
Definition: A thread is a single sequential flow of control within a program.
ÇϳªÀÇ thread¿¡¼´Â »õ·Î¿î °³³äÀÌ ¾ø´Ù.
ÁøÁ¤ÇÑ thread´Â ÇϳªÀÇ ¼øÂ÷ÀûÀÎ thread°¡ ¾Æ´Ï¶ó, ÇÁ·Î±×·¥³»¿¡¼
µ¿½Ã¿¡ ¿©·¯°¡Áö ÀÏÀ» ¼öÇàÇÏ´Â multiple thread¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù.
HotJava browser¿¡¼´Â ȸé scrollÀ» ÇÏ´Â µ¿¾È¿¡ image¸¦ load½ÃÅ°°í,
ÇÑÂÊ¿¡¼´Â animationÀ» ÇÏ°í ÀÖ°í, ´Ù¸¥ ÇÑÂÊ¿¡¼´Â sound°¡ ³ª¿Â´Ù.
¹è°æȸéÀ» Àдµ¿¾È »õ·Î¿î page¸¦ downloadÇÒ ¼ö ÀÖ´Ù.
ÀÌ°ÍÀº HotJava browser°¡ multithreaded applicationÀ̱⠶§¹®ÀÌ´Ù.
2.6.2 °£´ÜÇÑ Thread ¿¹Á¦.
´ÙÀ½Àº µÎ°³ÀÇ thread¸¦ »ý¼ºÇÏ¿© µ¶¸³ÀûÀ¸·Î ¼öÇàÇÏ´Â
°£´ÜÇÑ thread ÇÁ·Î±×·¥ÀÌ´Ù.
class TwoThreadsTest {
public static void main (String args[]) {
new SimpleThread("Jamaica").start();
new SimpleThread("Fiji").start();
}
}
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
SimpleThread´Â java.lang package¿¡¼ Á¦°øµÇ´Â Thread classÀÇ subclassÀÌ´Ù.
SimpleThread classÀÇ Ã¹¹ø° method´Â constructorÀÌ´Ù. Constructor¿¡¼´Â
´ÜÁö argument¸¦ StringÀ¸·Î ¹Þ¾Æ µéÀδÙ. ³Ñ°Ü¿Â argument´Â ÇÁ·Î±×·¥ÀÇ
ÀǹÌÀûÀ¸·Î º¼¶§ threadÀÇ À̸§À¸·Î ¾²ÀδÙ.
±×´ÙÀ½ method´Â run()ÀÌ´Ù. run() method´Â ¾î¶² thread¿¡¼µµ ÇÙ½ÉÀÌ µÇ´Â
methodÀÌ´Ù.
¿©±â¿¡¼´Â ´ÜÁö for loop¸¦ µ¹¸é¼ ¹Ýº¹È½¼ö¿Í threadÀÇ À̸§À» Ãâ·ÂÇÏ°í,
0~1Ãʵ¿¾È ÀÓÀÇ·Î ½®´ÙÀ½¿¡ ´Ù½Ã loop¸¦ µ·´Ù.
loop°¡ ³¡³ª¸é "DONE!"À̶õ ¸Þ½ÃÁö¸¦ ³²±â°í ¼öÇàÀ» ¸¶Ä£´Ù.
TwoThradsTest class´Â main() method¸¦ Á¦°øÇÏ°í ÀÖ´Ù. ¿©±â¿¡¼´Â
"Jamaica"¶ó´Â À̸§°ú "Fiji"¶ó´Â À̸§ÀÇ thread¸¦ °¢°¢»ý¼ºÇÑ´Ù.
main() method´Â °¢°¢ÀÇ threadÀÇ start() method¸¦ È£ÃâÇÏ°Ô µÊÀ¸·Î½á,
°¢°¢ÀÇ thread´Â ÀÛ¾÷À» ¼öÇàÇÏ°Ô µÈ´Ù.
´ÙÀ½Àº À§ ÇÁ·Î±×·¥ÀÇ ½ÇÇà °á°úÀÌ´Ù.
0 Jamaica
0 Fiji
1 Fiji
1 Jamaica
2 Jamaica
2 Fiji
3 Fiji
3 Jamaica
4 Jamaica
4 Fiji
5 Jamaica
5 Fiji
6 Fiji
6 Jamaica
7 Jamaica
7 Fiji
8 Fiji
9 Fiji
8 Jamaica
DONE! Fiji
9 Jamaica
DONE! Jamaica
ÀÌ¿Í°°ÀÌ Ãâ·ÂÀº °íÁ¤µÇ¾îÀÖÁö ¾Ê´Ù. ±×ÀÌÀ¯´Â µÎ°³ÀÇ SimpleThread thread°¡
µ¿½Ã¿¡ ½ÇÇàµÇ±â ¶§¹®ÀÌ´Ù.
run() methodÀÌ¿Ü¿¡ start(), sleep() method¿¡´ëÇؼ´Â
´ÙÀ½Àå¿¡¼ ¾Ë¾Æº¸±â·Î ÇÑ´Ù.
2.6.3 ThreadÀÇ ¼Ó¼º°ª.
Thread¸¦ ÀÌ¿ëÇÑ È¿À²ÀûÀÌ°í ¿À·ù°¡ ¾ø´Â ÇÁ·Î±×·¥À» ÀÛ¼ºÇϱâ À§Çؼ´Â
threadÀÇ ´Ù¾çÇÑ ºÎºÐ°ú, Java runtime system¿¡ ´ëÇؼ ¾Ë ÇÊ¿ä°¡ ÀÖ´Ù.
ThreadÀÇ body¸¦ ¾î¶»°Ô Á¤ÀÇÇØ¾ß ÇÏ°í, threadÀÇ life-cycle°ú, runtime systemÀÌ
¾î¶»°Ô thread¸¦ schedulingÇÏ´ÂÁö¿¡ ´ëÇؼ, thread group, daemon thread¿¡
´ëÇؼ´ëÇؼµµ ¾Ë¾Æ¾ß ÇÑ´Ù.
Java threadÀÇ body´Â java.lang package¿¡ ÀÖ´Â Thread class¿¡ ÀÇÇؼ
run() method¸¦ Á¤ÀÇÇÒ ¼ö ÀÖ°í, ¶Ç´Â Runnable object·Î »ý¼ºÇÏ´Â
µÎ°¡Áö ¹æ¹ýÀÌ ÀÖ´Ù.
2.6.3.1 ThreadÀÇ Body.
ThreadÀÇ ¸ðµç ÇàÀ§´Â threadÀÇ body¿¡ÀÖ´Â run() method¿¡ ÀÇÇؼ ÇàÇØÁø´Ù.
Thread classÀÇ subclass¸¦ Á¤ÀÇÇÔÀ¸·Î½á run() method¸¦ override ÇÒ ¼ö ÀÖ´Ù.
Thread°¡ »ý¼ºµÇ¾î ÃʱâÈ°¡ µÈÈÄ¿¡, runtime systemÀº run() method¸¦ È£ÃâÇÑ´Ù.
run() method´Â »ý¼ºµÈ thread°¡ ÇؾßÇÒ ÀÏÀ» Àü´ãÇÏ¿© ¼öÇàÇÑ´Ù.
º¸Åë threadÀÇ run() method´Â loop¸¦ µ·´Ù. ¿¹¸¦ µé¾î¼ AnimationÀº loop¸¦
µ¹¸é¼ ¿¬¼ÓÀûÀ¸·Î image¸¦ º¸¿©ÁØ´Ù. °¡²û threadÀÇ run() method´Â ¿À·£½Ã°£µ¿¾È
¿¬»êÀ» ÇÒ °æ¿ìµµ ÀÖ´Ù. ¿¹¸¦ µé¾î¼ sound³ª JPEG image¸¦ º¸¿©Áְųª download
ÇÏ´Â °æ¿ì¸¦ µé ¼ö ÀÖ´Ù.
´ÙÀ½Àº run() method¸¦ Á¤ÀÇÇÏ´Â 2°¡Áö ¹æ¹ýÀÌ´Ù.
2.6.3.1.1 - °£´ÜÇÑ thread ¿¹Á¦.
java.lang package¿¡ ÀÖ´Â Thread classÀÇ run() method¸¦ override ÇÑ´Ù.
´ÙÀ½ ÀÌÀü¿¡ 2.6.2¿¡¼ Á¤ÀÇÇß´ø ¿¹Á¦ÀÌ´Ù.
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
2.6.3.1.2 Clock Applet.
´ÙÀ½Àº Runnable interface¸¦ Á¦°øÇÏ´Â classÀÌ´Ù. ÀÌ°Í ¶ÇÇÑ java.lang package¿¡
Á¤ÀǵǾî ÀÖ´Ù.
À̹æ¹ýÀº thread¸¦ instantiate ½Ãų¶§ ÀÌ »õ·Î¿î threadÀÇ handleÀ»
Runnable classÀÇ instance¿¡ ³Ñ°ÜÁØ´Ù. Runnable class´Â ÀÌ thread¿¡
run() method¸¦ Á¦°øÇØÁØ´Ù.
´ÙÀ½ clock appletÀº threadÀÇ run() method¸¦ Áö¿øÇÏ´Â Runnable interface¸¦
ÀÌ¿ëÇÑ ¹æ¹ýÀÌ´Ù.
import java.awt.Graphics;
import java.util.Date;
public class Clock extends java.applet.Applet implements Runnable {
Thread clockThread;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
public void run() {
while (clockThread != null) {
repaint();
try {
clockThread.sleep(1000);
} catch (InterruptedException e){
}
}
}
public void paint(Graphics g) {
Date now = new Date();
g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);
}
public void stop() {
clockThread.stop();
clockThread = null;
}
}
Clock class´Â Applet classÀ¸·ÎºÎÅÍ ÆÄ»ýµÇ¾ú´Ù. ±×·¯³ª Clock appletÀº
½ÇÇàÇÏ´Â µ¿¾È¿¡ ȸéÀ» ¿¬¼ÓÀûÀ¸·Î °»½ÅÇϴµ¥ µû·Î ½Ã°£ÀÌ °É¸®Áö ¾Ê°Ô
Çϱâ À§Çؼ thread¸¦ ÀÌ¿ëÇÑ´Ù.
¶ÇÇÑ, Java language´Â ´ÙÁß »ó¼Ó(muliple-inheritance)¸¦ Áö¿øÇÏÁö ¾Ê±â ¶§¹®¿¡
Applet°ú, Thread class¸¦ µ¿½Ã¿¡ »ó¼ÓÇÒ ¼ö ¾ø´Ù.
µû¶ó¼, Clock class´Â theadÀÇ ±â´É Áö¿øÇϱâ À§Çؼ
Runnable interface¸¦ »ç¿ëÇÏ°í ÀÖ´Ù.
AppletÀÚü°¡ thread°¡ ¾Æ´Ï°í, Java ȣȯ browser³ª, appletviewer¿¡¼
ÀÚµ¿À¸·Î thread¸¦ »ý¼ºÇØÁÖ´Â °ÍÀÌ ¾Æ´Ï¹Ç·Î, appletÀÌ thread°¡ ÇÊ¿äÇϸé
µû·Î thread¸¦ »ý¼ºÇØ¾ß ÇÑ´Ù.
Clock applet¿¡¼´Â ÀÚÁÖ È¸éÀ» °»½ÅÇϱâ À§Çؼ ÇϳªÀÇ thread°¡ ÇÊ¿äÇѵ¥,
±×ÀÌÀ¯´Â ȸ鰻½ÅÀ» À§Çؼ µû·Î ½Ã°£ÀÌ °É¸®Áö ¾Ê°Ô Çϱâ À§Çؼ´Ù.
Runnable interface¸¦ »ç¿ëÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.
class Clock extends Applet implements Runnable {
Runnable interface¿¡¼´Â argument¿Í return°ªÀÌ ¾ø´Â run() method¸¦ Á¤ÀÇÇÑ´Ù.
Clock class¿¡¼´Â Runnable interface¸¦ ó¸®Çϱâ À§Çؼ, run() method¸¦
Á¤ÀÇÇØ¾ß ÇÑ´Ù.
run() method¸¦¾Ë¾Æº¸±â Àü¿¡ ¸ÕÀú ¸î°¡Áö ÇÊ¿äÇÑ °ÍµéÀ» ¾Ë¾Æº»´Ù.
Applet¿¡¼ appletÀ» loadÇؼ ½ÇÇàÇÒ¶§ Á¦ÀϸÕÀú start() method°¡ ½ÇÇàµÈ´Ù.
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
Clock applet¿¡¼ clockThread¶ó´Â thread¸¦ °¡Áö°í Àִµ¥,
start() method¿¡¼´Â Á¦ÀϸÕÀú clockThread°¡ null ÀÎÁö °Ë»çÇÑ´Ù.
¸¸¾à clockThread°¡ nullÀ̸é, appletÀº »õ·Î¿î thread¸¦ »ý¼ºµÈ´Ù.
nullÀÌ ¾Æ´Ï¸é, appletÀÌ ÀÌ¹Ì ½ÇÇàµÇ°í Àִٴ°ÍÀÌ´Ù.
´ÙÀ½Àº thread¸¦ »õ·Ó°Ô »ý¼ºÇÏ´Â ¹æ¹ýÀÌ´Ù.
clockThread = new Thread(this, "Clock");
¿©±â¿¡¼ ù¹ø° argument·Î this¸¦ ³Ñ°Ü ¹Þ¾Ò´Ù.
ÀÌ°ÍÀº Runnable interface¿¡¼ »ý¼ºµÈ threadÀÇ targetÀ» ³ªÅ¸³»´Â °ÍÀÌ´Ù.
Áï, ÀÌ·¸°Ô ÇÔÀ¸·Î½á Thread constructor´Â target Runnable objectÀÇ
run() method¸¦ ¾ò¾î³»´Â °ÍÀÌ´Ù.
µÎ¹ø° argument´Â ´ÜÁö threadÀÇ À̸§ÀÌ´Ù.
¸¸¾à, Clock appletÀÌ ½ÇÇàµÇ°í ÀÖ´Â page¸¦ ±×¸¸µÎ°í ¶°³ª°Ô µÇ¸é
appletÀÌ ½ÇÇàµÇ°í ÀÖ´Â applicationÀº appletÀÇ stop() method¸¦
È£ÃâÇÏ°Ô µÇ°í, thread´Â nullÀÌ µÈ´Ù.
´ÙÀ½ÀÇ ¿¹Á¦¿¡¼´Â ½Ã°£À» °»½ÅÇÏ´Â ÀÛ¾÷À» ±×¸¸µÎ°Ô µÈ´Ù.
public void stop() {
clockThread.stop();
clockThread = null;
}
¸¸¾à Clock appletÀÌ ÀÖ´Â page°¡ ´Ù½Ã ÂüÁ¶µÇ¸é, start() method¸¦ ´Ù½Ã
È£ÃâµÇ°í, »õ·Î¿î thread¸¦ »ý¼ºÇÏ°í ÇÔÀ¸·Î½á ½Ã°è°¡ µ¹¾Æ°¡°Ô µÈ´Ù.
Clock classÀÇ ÇÙ½É ºÎºÐÀÎ run() method´Â ´ÙÀ½°ú °°´Ù.
public void run() {
while (clockThread != null) {
repaint();
try {
clockThread.sleep(1000);
} catch (InterruptedException e){
}
}
}
¾Õ¿¡¼ »ìÆ캻°Í°ú °°ÀÌ appletÀÌ stopµÇ¸é, appletÀº clockThread¸¦ stop½ÃÅ°°í
clockThread¸¦ null·Î ¸¸µç´Ù. À̶§ run() method´Â clockThread°¡
nullÀÌ ¾Æ´Ò¶§¸¸ loop¸¦ µ·´Ù. Loop¾È¿¡¼´Â, appletÀº ȸéÀ» repaintÇϸç
thread¸¦ 1Ãʵ¿¾È ½¬°Ô ÇÑ´Ù. AppletÀÇ repaint() method´Â appletÀÇ È¸éÀ»
Á÷Á¢ÀûÀ¸·Î Ãâ·Â½ÃÄÑÁÖ´Â paint() method¸¦ È£ÃâÇÑ´Ù.
Clock appletÀÇ paint() method´Â ÇöÀç ½Ã°£À» ¾ò¾î³»¾î ȸ鿡 Ãâ·ÂÇØÁØ´Ù.
public void paint(Graphics g) {
Date now = new Date();
g.drawString(now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds(), 5, 10);
}
¸¸¾à class°¡ Thread°¡ ¾Æ´Ï ´Ù¸¥ class·Î ºÎÅÍ ÆÄ»ýµÇ¾ú´Ù¸é,
ÀϹÝÀûÀ¸·Î RunnableÀ» »ç¿ëÇÑ´Ù.
2.6.3.2 ThreadÀÇ »óÅÂ, ´Ü°è.
Thread´Â ¸î°¡Áö »óŸ¦ °¡Áö´Âµ¥, thread´Â running, sleep, dead µî
°¢ »óŸ¶´Ù ÇØ¾ß ÇÏ´Â ÀÏÀÌ ´Ù¸£´Ù.
´ÙÀ½ diagramÀº threadÀÇ °¢ ´Ü°è¿Í ¼öÇàµÇ´Â method¸¦ º¸¿©ÁÖ°í ÀÖ´Ù.
´ÙÀ½ ¹®ÀåÀº »õ·Î¿î thread¸¦ »ý¼ºÇÑ´Ù. ±×·¯³ª, startÇÏÁö´Â ¾Ê°í
´ÙÀ̾î±×·¥ÀÇ "New Thread"»óÅ¿¡ ³õÀÌ°Ô µÈ´Ù.
Thread myThread = new MyThreadClass();
Thread°¡ "New Thread"»óÅ¿¡ ³õÀÌ°Ô µÇ¸é, ÀÌ°ÍÀº ºñ¾îÀÖ´Â Thread objectÀÌ´Ù.
¾ÆÁ÷ system resoruce¸¦ Ç×´çÇÏÁö ¾Ê¾Ò´Ù. ±×·¯¹Ç·Î, ÀÌ »óÅ¿¡¼´Â
thread¸¦ ¿ÀÁ÷ start, stop½Ãų ¼ö¸¸ ÀÖ´Ù.
´ÙÀ½ÀÇ 2°³ÀÇ ¹®ÀåÀ» º¸±â·Î ÇÑ´Ù.
Thread myThread = new MyThreadClass();
myThread.start();
start() method´Â thread¸¦ run½ÃÅ°±â À§Çؼ system resource¸¦ ÇÒ´çÇÏ°í,
schedulingÀ» Çϸç, run() method¸¦ È£Ãâ ÇÑ´Ù.
ÀÌ·¯ÇÑ »óÅ´ "Running"À̶ó±â º¸´Ù´Â "Runnable" »óŶó°í ÇÑ´Ù.
¿Ö³ÄÇϸé, ½ÇÁö·Î thread°¡ ¾ÆÁ÷ ½ÇÇàµÇ´Â°ÍÀÌ ¾Æ´Ï±â ¶§¹®ÀÌ´Ù.
´ëºÎºÐÀÇ computer´Â µ¿½Ã¿¡ thread°¡ "Runnable" Çϴ°ÍÀº ºÒ°¡´ÉÇϱ⠶§¹®¿¡
Java runtime systemÀº processor´Â °øÀ¯Çϱâ À§Çؼ "Runnable"ÇÑ ¸ðµç
thread¸¦ schedulingÇؾßÇÑ´Ù.
À̶§ thread´Â ¿ì¼±¼øÀ§°¡ Àִµ¥ ÀÌ°ÍÀº ´ÙÀ½¿¡ ¾Ë¾Æº¸±â·Î ÇÑ´Ù.
´ÙÀ½ÀÇ 4°¡Áö »óȲÀÌ ÀϾ¶§ thread´Â "Not Runnable"»óÅ¿¡ ³õÀÌ°Ô µÈ´Ù.
-
someone calls its
suspend()
method
-
someone calls its
sleep()
method
-
the thread uses its
wait()
method to wait on a condition variable
-
the thread is blocking on I/O.
¿¹¸¦ µé¾î¼ ´ÙÀ½ÀÇ ±½Àº ±Û¾¾¸¦ º¼ ¼ö ÀÖ´Ù.
Thread myThread = new MyThreadClass();
myThread.start();
try {
myThread.sleep(10000);
} catch (InterruptedException e){
}
À§ÀÇ ¿¹´Â thread¸¦ 10Ãʵ¿¾È ÀáÀç¿ì´Â °ÍÀÌ´Ù.
ÀÌ 10Ãʵ¿¾ÈÀº processor¸¦ »ó¿ëÇÒ ¼ö ÀÖÁö¸¸, myThread´Â ½ÇÇàµÇÁö ¾Ê´Â´Ù.
10ÃÊ°¡ Áö³ª¸é myThread´Â ´Ù½Ã ½ÇÇàÇÒ ¼ö ÀÖµµ·Ï "Runnable"»óÅ¿¡ ³õÀδÙ.
"Not Runnable"»óÅ¿¡ µé¾î°¡±âÀü¿¡ thread°¡ ´Ù½Ã "Runnable"»óÅ·ΠµÇµ¹¾Æ°¡±â
À§Çؼ escape route°¡ ÀÖ´Ù. ¿¹¸¦ µé¾î¼, thread°¡ sleepÇϱâ Àü¿¡ "Runnable"
»óÅ·Πµ¹¾Æ¿À±â À§Çؼ ¸îÃʵ¿¾È ½¯°ÍÀÎÁö ¸í½Ã¸¦ Çϴ°ÍÀÌ´Ù.
´ÙÀ½Àº "Not Runnable"»óÅ·Πµé¾î°¡±âÀü¿¡ "Runnable"»óÅ·Πµ¹¾Æ¿À±â À§ÇÑ
escape route¹æ¹ýÀ» ¼Ò°³ÇÑ´Ù.
-
If a thread has been put to sleep, then the specified number of milliseconds must elapse.
-
If a thread has been suspended, then someone must call its
resume()
method.
-
If a thread is waiting on a condition variable, whatever object owns the variable must relinquish it by calling either
notify()
or notifyAll()
.
-
If a thread is blocked on I/O, then the specified I/O command must complete.
Thread´Â run() method¸¦ ¸¶Ä¡°í ÀÚ¿¬½º·´°Ô Á×À» ¼ö ÀÖ´Ù.
public void run() {
int i = 0;
while (i < 100) {
i++;
System.out.println("i = " + i);
}
}
¶Ç´Â, stop() method¸¦ È£ÃâÇÔÀ¸·Î½á thread¸¦ Á×ÀÏ ¼ö ÀÖ´Ù.
Thread myThread = new MyThreadClass();
myThread.start();
try {
Thread.currentThread().sleep(10000);
} catch (InterruptedException e){
}
myThread.stop();
ThreadÀÇ method¸¦ È£ÃâÇߴµ¥, thread°¡ method¸¦ ½ÇÇà½ÃÅ°±âÀ§ÇÑ state·Î
ÀüȯÀÌ µÇÁö ¾ÊÀ¸¸é, runtime systemÀº IllegalThreadStateExecptionÀ»
½ÇÇà½ÃŲ´Ù. ¿¹¸¦ µé¾î¼, "Runnable"ÇÏÁö ¾ÊÀº threadÀÇ suspend()¸¦ È£ÃâÇϸé
IllegalThreadStateExceptionÀÌ ½ÇÇàµÈ´Ù.
ThreadÀÇ method¸¦ Á¤ÀÇÇÒ¶§´Â exceptionÀ̳ª catch, ¶Ç´Â uncaughtµÈ
exceptionÀ» ó¸®Çϱâ À§ÇÑ method¸¦ Á¤ÀÇÇØ¾ß ÇÑ´Ù.
ThreadÀÇ state¸¦ ¾Ë¾Æº¸±â À§ÇÑ Thread classÀÇ programming interface·Î
isAlive() method°¡ Á¦°øµÈ´Ù.
isActive() method´Â thread°¡ ½ÃÀÛÇؼ stopµÇÁö ¾Ê¾ÒÀ¸¸é true¸¦ returnÇÑ´Ù.
±×·¯¹Ç·Î, ¸¸¾à isActive() method°¡ false¸¦ returnÇϸé, thread´Â
"New Thread"³ª "Dead"»óÅ¿¡ ÀÖÀ½À» ¾Ë ¼ö ÀÖ°í, true¸¦ returnÇϸé
thread´Â "Runnable"À̳ª "Not Runnable"»óÅ¿¡ ÀÖÀ½À» ¾Ë ¼ö ÀÖ´Ù.
Áï, ¿ì¸®´Â "New Thread"°ú "Dead"»óŸ¦ ±»ÀÌ ±¸º°ÇÒ ÇÊ¿ä¾ø°í,
"Runnable"°ú "Not Runnable"»óŸ¦ ±¸º°ÇÒ ÇÊ¿ä°¡ ¾ø´Ù.
2.6.3.3 ThreadÀÇ ¿ì¼±¼øÀ§.
Thread´Â concurrentÇÏ°Ô ½ÇÇàµÈ´Ù´Â °ÍÀ» ¾Ë°í ÀÖ´Ù.
Áï, ´ëºÎºÐÀÇ ÄÄÇ»ÅÍ´Â ÀϹÝÀûÀ¸·Î CUP Çϳª¸¦ °¡Áö°í ÀÖÀ¸¹Ç·Î,
cuncurrency¿¡¼µµ »ç½Ç»ó Çѹø¿¡ ¿ÀÁ÷ ÇϳªÀÇ thread¸¸ÀÌ ½ÇÇàµÈ´Ù.
ÀÌ·¸°Ô, ÇϳªÀÇ CUP¿¡¼ ¿©·¯°³ÀÇ thread¸¦ ½ÇÇà ½ÃÅ°±â À§Çؼ
schedulingÀ̶ó´Â°ÍÀ» ÇÑ´Ù.
Java runtime system¿¡¼´Â ÀϹÝÀûÀ¸·Î À߾˷ÁÁø scheduling algorithmÀ¸·Î
fixed priority schedulingÀ» »ç¿ëÇÏ°í ÀÖ´Ù.
Java thread°¡ »ý¼ºµÉ¶§, ±× thread´Â ÀÚ½ÅÀ» »ý¼ºÇÑ threadÀÇ ¿ì¼±¼øÀ§¸¦
»ó¼Ó ¹Þ´Â´Ù. ¶ÇÇÑ, thread°¡ »ý¼ºµÈ ÈÄ¿¡´Â setPriority() method¸¦ ÀÌ¿ëÇÏ¿©
threadÀÇ ¿ì¼±¼øÀ§¸¦ Á¤ÇÒ ¼ö ÀÖ´Ù. ThreadÀÇ ¿ì¼±¼øÀ§´Â MIN_PRIORITY¿Í
MAX_PRIORITYÁß°£ÀÇ °ªÀ» °¡Áú ¼ö ÀÖ´Ù.
¿©·¯°³ÀÇ thead°¡ Á¸ÀçÇÒ¶§ runtime systemÀº ¿ì¼±¼øÀ§°¡ ³ôÀº thread¸¦
¼±ÅÃÇÏ¿© "Runnable"»óÅ·ΠÀüȯÇÏ¿© ½ÇÇà ½ÃŲ´Ù. ¿ì¼± ¼øÀ§°¡ ³ôÀº thread°¡
yeild, stopµÉ¶§ ¶Ç´Â "Not Runnable"»óÅ·ΠµÉ¶§¸¸, ¿ì¼±¼øÀ§°¡ ³·Àº thread°¡
½ÇÇà µÈ´Ù. ¸¸¾à ¿ì¼±¼øÀ§°¡ °°Àº CUP¸¦ ±â´Ù¸®´Â thread 2°³°¡ Á¸ÀçÇϸé
round-robin ¹æ¹ýÀ¸·Î ¼±ÅÃÇÑ´Ù.
Java runtime systemÀÇ thread scheduling algorithmÀº ¶ÇÇÑ preemptiveÀÌ´Ù.
Áï, ÇöÀç "Runable"ÇÑ theadº¸´Ù ¿ì¼±¼øÀ§°¡ ³ôÀº thread°¡ ±â´Ù¸®°í ÀÖ´Ù¸é,
runtime systemÀº »õ·Î¿î ¿ì¼±¼øÀ§°¡ ³ôÀº thread¸¦ ¼±ÅÃÇÏ¿© ½ÇÇà ½ÃŲ´Ù.
À̶§, ÀÌ·± »õ·Î¿î ¿ì¼±¼øÀ§°¡ ³ôÀº thread´Â ´Ù¸¥ thread¸¦
Á¡À¯(preempt) Çß´Ù°í ÇÑ´Ù.
Java runtime systemÀÇ thread scheduling¹æ¹ýÀº ´ÙÀ½ÀÇ ¹ýÄ¢À¸·Î
¿ä¾à ÇÒ ¼ö ÀÖ´Ù.
Rule: At any given time, the highest priority runnable thread is running.
import java.awt.*;
public class RaceApplet extends java.applet.Applet implements Runnable {
final static int NUMRUNNERS = 2;
final static int SPACING = 20;
Runner runners[] = new Runner[NUMRUNNERS];
Thread updateThread;
public void init() {
String raceType = getParameter("type");
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new Runner();
if (raceType.compareTo("unfair") == 0)
runners[i].setPriority(i+1);
else
runners[i].setPriority(2);
}
if (updateThread == null) {
updateThread = new Thread(this, "Thread Race");
updateThread.setPriority(NUMRUNNERS+1);
}
}
public boolean mouseDown(java.awt.Event evt, int x, int y) {
if (!updateThread.isAlive())
updateThread.start();
for (int i = 0; i < NUMRUNNERS; i++) {
if (!runners[i].isAlive())
runners[i].start();
}
return true;
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
g.fillRect(0, 0, size().width, size().height);
g.setColor(Color.black);
for (int i = 0; i < NUMRUNNERS; i++) {
int pri = runners[i].getPriority();
g.drawString(new Integer(pri).toString(), 0, (i+1)*SPACING);
}
update(g);
}
public void update(Graphics g) {
for (int i = 0; i < NUMRUNNERS; i++) {
g.drawLine(SPACING, (i+1)*SPACING, SPACING + (runners[i].tick)/1000, (i+1)*SPACING);
}
}
public void run() {
while (updateThread != null) {
repaint();
try {
updateThread.sleep(10);
} catch (InterruptedException e) {
}
}
}
public void stop() {
for (int i = 0; i < NUMRUNNERS; i++) {
if (runners[i].isAlive()) {
runners[i].stop();
runners[i] = null;
}
}
if (updateThread.isAlive()) {
updateThread.stop();
updateThread = null;
}
}
}
À§ÀÇ ¿¹Á¦´Â ¿ì¼±¼øÀ§°¡ ¼·Î ´Ù¸¥ "runner" thread°¡ animateÇÏ´Â appletÀÌ´Ù.
Applet¿¡ Mouse·Î clickÇϸé, µÎ°³ÀÇ runner°¡ ½ÃÀ۵ȴÙ.
ù¹ø° runner´Â "1"À̶ó´Â À̸§À» °¡Áö°í ÀÖ°í, ¿ì¼±¼øÀ§°¡ 1ÀÌ´Ù.
µÎ¹ø° runner´Â "2"¶ó´Â À̸§À» °¡Áö°í ÀÖ°í, ¿ì¼± ¼øÀ§°¡ 2ÀÌ´Ù.
¾Æ·¡ ¸¦ ¸¶¿ì½º·Î clickÇϸé runner appletÀÌ ½ÇÇà µË´Ï´Ù.
´ÙÀ½Àº µÎ runnerÀÇ run() methodÀÌ´Ù.
public int tick = 1;
public void run() {
while (tick < 400000) {
tick++;
}
}
run() method´Â ´ÜÁö 1 ~ 400,000°¡Áö ¼¾´Ù. Instance variable tickÀº
publicÀÌ´Ù. ¿Ö³ÄÇϸé, applet¿¡¼ ÀÌ °ªÀ» ÂüÁ¶Çϱ⠶§¹®ÀÌ´Ù.
µÎ°³ÀÇ runner threadÀÌ¿Ü¿¡, appletÀº ȸ鿡 ¼±À» ±×·ÁÁÖ´Â 3¹ø° thread¸¦
Çϳª ´õ °¡Áö°í ÀÖ´Ù. ¼±À» ±×¸®´Â threadÀÇ run() method´Â ¹«ÇÑ loop¸¦ µ¹°í ÀÖ´Ù.
; µÎ°³ÀÇ runnerÀÇ tickÀÇ °ªÀ» °è»êÇÏ¿© ȸ鿡 ±æÀ̸¸Å ¼±À» ±ß°í 10 millisecond
µ¿¾È sleepÇÑ´Ù. ÀÌ threadÀÇ ¿ì¼±¼øÀ§´Â 3À¸·Î½á ´Ù¸¥ 2°³ÀÇ runnerº¸´Ù ´õ ³ô´Ù.
±×·¯¹Ç·Î drawing thread°¡ 10 milliseconds Áö³ª¼ ±ú¾î³ª¸é, ÀÌ ¶§ ¿ì¼±¼øÀ§°¡
³ôÀº runner°¡ ½ÇÇàµÇ°í ȸ鿡 ¼±À» ±ß´Â´Ù.
¾Ë´Ù½ÃÇÇ ÀÌ°ÍÀº Á¤´çÇÑ °æÁÖ°¡ µÇÁö ¸øÇÑ´Ù. ¿Ö³ÄÇϸé, ¾î´À runnerÇϳª°¡
´Ù¸¥ runnerº¸´Ù ¿ì¼±±ÇÀÌ ´õ ³ô±â ¶§¹®ÀÌ´Ù. ¸Å¹ø ¿ì¼±¼øÀ§°¡ ³·Àº runner´Â
¿ì¼±¼øÀ§°¡ ³ôÀº runner¿¡°Ô CPU¸¦ »¯±ä´Ù.
´ÙÀ½Àº ¿ì¼±¼øÀ§°¡ ¼·Î °°Àº runner appletÀÌ´Ù. ¸¶¿ì½º·Î clickÇϸé
°æÁÖ°¡ ½ÃÀ۵ȴÙ.
À̹ø¿¡´Â µÎ thread(runner)°¡ °°Àº ¿ì¼±±ÇÀ» °¡Áö°íÀֱ⠶§¹®¿¡, scheduler´Â
round-robin ±â¹ýÀ¸·Î Â÷·Ê´ë·Î ½ÇÇà½ÃŲ´Ù.
¾î¶² system¿¡¼´Â ¿ì¼±¼øÀ§°¡ °°Àº "Runnable" thread°¡ ¿©·¯°³ ÀÖÀ»¶§
¼·Î °æÀïÀ» ÇÏ¸é¼ ÀÏÀ» ¼öÇàÇÑ´Ù. ´ÙÀ½Àº ¿ì¼±¼øÀ§°¡ °°Àº µÎ°³ÀÇ
thread¸¦ »ý¼ºÇÏ¿© ½ÇÇà ½ÃÅ°´Â ÇÁ·Î±×·¥ÀÌ´Ù.
class SelfishRunner extends Thread {
public int tick = 1;
public int num;
SelfishRunner(int num) {
this.num = num;
}
public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0) {
System.out.println("Thread #" + num + ", tick = " + tick);
}
}
}
}
class RaceTest {
final static int NUMRUNNERS = 2;
public static void main(String args[]) {
SelfishRunner runners[] = new SelfishRunner[NUMRUNNERS];
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new SelfishRunner(i);
runners[i].setPriority(2);
}
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i].start();
}
}
}
½ÃºÐÇÒ(Time-Slicing) system¿¡¼´Â ´ÙÀ½°ú °°Àº °á°ú¸¦ º¸¿©ÁØ´Ù.
Thread #1, tick = 50000
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #1, tick = 100000
Thread #1, tick = 150000
Thread #1, tick = 200000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #1, tick = 250000
Thread #0, tick = 250000
Thread #0, tick = 300000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #0, tick = 350000
Thread #0, tick = 400000
Thread #1, tick = 400000
ÇÏÁö¸¸, non-time-sliced system¿¡¼´Â ´ÙÀ½°ú °°Àº °á°ú°¡ ³ª¿Â´Ù.
Thread #0, tick = 50000
Thread #0, tick = 100000
Thread #0, tick = 150000
Thread #0, tick = 200000
Thread #0, tick = 250000
Thread #0, tick = 300000
Thread #0, tick = 350000
Thread #0, tick = 400000
Thread #1, tick = 50000
Thread #1, tick = 100000
Thread #1, tick = 150000
Thread #1, tick = 200000
Thread #1, tick = 250000
Thread #1, tick = 300000
Thread #1, tick = 350000
Thread #1, tick = 400000
À§¿Í°°Àº ÀÌÀ¯´Â, time-sliced system¿¡¼´Â time slot°ú ¿ì¼±¼øÀ§¿¡ µû¶ó¼
CPU¸¦ °øÀ¯ÇÑ´Ù. Áï, Á¤ÇØÁø ½Ã°£µ¿¾È ¹ø°¥¾Æ°¡¸ç ½ÇÇàµÇ°í, ¿ì¼±¼øÀ§°¡ ³ôÀº °ÍÀº
CPU¸¦ Á¡À¯ÇÒ ¼ö ÀÖµµ·Ï µÇ¾îÀÖ´Ù. Thread°¡ ¾î¶»°Ô scheduleµÉÁö ¸ð¸§À¸·Î,
½ÇÇà °á°úÀÇ ¼ø¼´Â ¿¹Ãø ÇÒ ¼ö ¾ø°Ô µÈ´Ù.
ÇÏÁö¸¸, non-time-sliced system¿¡¼´Â óÀ½ ¿ì¼±¼øÀ§°¡ ³ôÀº thread¸¦ ¼±ÅÃÇÏ¿©
threadÀÚ½ÅÀÌ ½º½º·Î sleeping, yielding, finlishÇÏ¿© CPU¸¦ ³»³õ°Å³ª,
¿ì¼±¼øÀ§°¡ ³ôÀº °ÍÀÌ CPU¸¦ Á¡À¯ÇÒ¶§ ±îÁö °è¼Ó ½ÇÇà µÈ´Ù.
´ÙÀ½ ¿¹´Â yield() method¸¦ ÀÌ¿ëÇÏ¿© ÀÚ¹ßÀûÀ¸·Î CPU¸¦ ¾çº¸ÇÑ´Ù. ¸¸¾à
"Runnable" state¿¡ ÀÖ´Â thread°¡ ¾øÀ¸¸é, yield´Â ¹«½ÃµÈ´Ù.
class PoliteRunner extends Thread {
public int tick = 1;
public int num;
PoliteRunner(int num) {
this.num = num;
}
public void run() {
while (tick < 400000) {
tick++;
if ((tick % 50000) == 0) {
System.out.println("Thread #" + num + ", tick = " + tick);
yield();
}
}
}
}
class RaceTest2 {
final static int NUMRUNNERS = 2;
public static void main(String args[]) {
PoliteRunner runners[] = new PoliteRunner[NUMRUNNERS];
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i] = new PoliteRunner(i);
runners[i].setPriority(2);
}
for (int i = 0; i < NUMRUNNERS; i++) {
runners[i].start();
}
}
}
¿ä¾à
- ´ëºÎºÐÀÇ systemÀº CPU¸¦ Çϳª¸¸ °¡Áö°í Àֱ⠶§¹«¿¡, ¿©·¯°³ÀÇ thread´Â
CPU¸¦ °øÀ¯ÇÏ¸ç »ç¿ëÇÑ´Ù. ±×·¸°Ô ÇϱâÀ§Çؼ´Â schedulingÀ» Çϴµ¥,
Java runtime systemÀº °£´ÜÇÏ°í °·ÂÇÑ fixed priority schedulingÀ» »ç¿ëÇÑ´Ù.
- °¢°¢ÀÇ Java thread´Â ¼ýÀÚ·ÎµÈ ¿ì¼±¼øÀ§¸¦ °¡Áö´Âµ¥, MIN_PRIORITY¿Í
MAX_PRIORITYÁß°£ÀÇ °ªÀ» °¡Áø´Ù. ¿©·¯°³ÀÇ ½ÇÇà °¡´ÉÇÑ thread°¡ ½ÇÇàÇϱ⸦
ÁغñÇÏ°í ÀÖÀ»¶§, ¿ì¼±¼øÀ§°¡ ³ôÀº therad°¡ ¼±ÅÃµÇ¾î ½ÇÇà µÈ´Ù. ¸¸¾à,
¾î¶°ÇÑ ÀÌÀ¯·Î ½ÇÇàÀ» Áß´ÜÇÏ°Ô µÉ¶§¸¸, ¿ì¼±¼øÀ§°¡ ³·Àº thread°¡ ½ÇÇàµÈ´Ù.
- CPUÀÇ schedulingÀº preemptiveÀÌ´Ù. ¸¸¾à ¿ì¼±¼øÀ§°¡ ³ôÀº thread°¡
½ÇÇàÇϱ⸦ ±â´Ù¸®¸ç ÁغñÇÏ°í ÀÖ´Ù¸é, ¿ì¼±¼øÀ§°¡ ³ôÀº thread´Â Áï½Ã
scheduleµÈ´Ù.
- Java runtime system¿¡¼ ÇöÀç ½ÇÇà ÁßÀÎ thread¿Í ¿ì¼±¼øÀ§°¡ °°Àº thread°¡
½ÇÇàÀ» ÁغñÇÏ°í ÀÖ´Ù¸é nonpreempiveÇÒ ¼ö ÀÖ´Ù. Áï, Java runtimeÀº
time-slice ÇÏÁö ¾Ê´Â´Ù. ±×·¯³ª, system¿¡¼´Â JavaÀÇ Thread class¸¦
¼öÇàÇÔÀ¸·Î½á time slicingÀ» Á¦°øÇÒ ¼ö ÀÖ´Ù. Time-slicing¿¡ ÀÇÁ¸ÇÏ´Â
programÀ» Á¦ÀÛÇÏÁö ¾Ê´Â°ÍÀÌ ÁÁ´Ù.
- ¶ÇÇÑ, ¾ðÁ¦ ¾îµð¼³ª yield() method¸¦ È£ÃâÇÏ¿© ½ÇÇàÀ» Æ÷±âÇÒ ¼ö°¡ ÀÖ´Ù.
Thread´Â ¿ì¼±¼øÀ§°¡ °°Àº thread¿¡°Ô CPU¸¦ ¾çº¸ÇÒ ¼ö ÀÖ°í, ¿ì¼±¼øÀ§°¡ ³·Àº
thread´Â ¹«½ÃÇÑ´Ù.
2.6.3.4 Daemon Thread.
¸ðµç Java thread´Â daemon thread°¡ µÉ ¼ö ÀÖ´Ù. Daemon thread´Â
°°Àº process¾È¿¡¼ ´Ù¸¥ thread³ª object¸¦ serviceÇØÁÖ´Â daemon threadÀÌ´Ù.
¿¹¸¦ µé¾î¼, HotJava browser´Â image¸¦ ÇÊ¿ä·ÎÇÏ´Â thread³ª object¿¡°Ô
filesystemÀ̳ª networkÀ¸·ÎºÎÅÍ image¸¦ ÀоîÁÖ´Â ±â´ÉÀ» ÇÏ´Â
"Background Image Reader"¶ó´Â daemon thread¸¦ °¡Áö°í ÀÖ´Ù.
Daemon thread´Â ÀüÇüÀûÀ¸·Î °°Àº application¿¡ ÀÖ´Â object¸¦ À§Çؼ
service¸¦ Á¦°øÇÏ´Â µ¶¸³ÀûÀÎ threadÀÌ´Ù. Daemon thread¸¦ À§ÇÑ run() method´Â
ÀüÇüÀûÀ¸·Î serviceÀÇ ¿äûÀ» ±â´Ù¸®¸ç ¹«ÇÑ loop¸¦ ¼öÇàÇÑ´Ù.
Process¿¡ ¿ÀÁ÷ daemon thread È¥ÀÚ¸¸ ³²¾ÆÀÖÀ»¶§, interpreter´Â exitÇÑ´Ù.
Áï À̶§´Â, service¸¦ ¿äûÇÏ´Â thread°¡ ´õÀÌ»ó Á¸ÀçÇÏÁö ¾Ê°í, daemon thread¸¸ÀÌ
Á¸ÀçÇÒ¶§¸¦ ¸»ÇÑ´Ù.
true¸¦ parameter·Î ÇÏ¿© setDaemon() method¸¦ È£ÃâÇϸé, thread´Â daemon
thread°¡ µÈ´Ù. isDaemon() method¸¦ »ç¿ëÇÏ¿© thread°¡ daemon threadÀÎÁö
¾Ë¾Æº¼ ¼ö ÀÖ´Ù.
2.6.3.5 Thread Group.
2.6.4 Multithreaded Progmrams.
°¡²û¾¿, thread´Â data¸¦ °øÀ¯ÇÒ ¶§°¡ ÀÖ´Ù. ¿¹¸¦ µé¾î¼, ¾î¶² thread°¡ file¿¡
data¸¦ ¾²°í, °°Àº ½Ã°£¿¡ ´Ù¸¥ thread´Â °°Àº file¿¡¼ data¸¦ Àоî¿Â´Ù°í
°¡Á¤Çغ¸ÀÚ. À̶§, Á¤º¸¸¦ °øÀ¯Çϴµ¥ ¿Ã¹Ù¸¥ °á°ú¸¦ ¾ò±â À§Çؼ thread´Â
µ¿±âÈ(synchronize)µÇ¾î¾ß ÇÑ´Ù.
µÎ°³ÀÇ thread°¡ ¼·Î ¾î¶²ÇÑ Á¶°ÇÀ» ¼·Î¿¡°Ô ¿ä±¸ÇÏ¸ç ±â´Ù¸®¸é¼
¹Ý¸é¿¡ Á¶°ÇÀÌ ¸¸Á·ÇÏÁö ¾Ê±â ¶§¹®¿¡ ¾Æ¹«Àϵµ ÇÏÁö ¾Ê´Âµ¥, ´Ù½Ã ÀÌ°ÍÀº
¼·ÎÀÇ Á¶°ÇÀ» ÃæÁ·½ÃÄÑÁÖÁö ¸øÇØÁÖ´Â »óȲÀÌ ¹ß»ýÇÏ°Ô µÇ´Âµ¥, ÀÌ·¯ÇÑ »óŸ¦
deadlockÀ̶ó°í Çϸç, ´ÙÀ½¿¡ ¸¸ÂùÁßÀΠöÇÐÀÚ(dining philosopher)¸¦ ¿¹·Îµé¾î
¼³¸íÇϸç, deadlockÀ» ÇÇÇÏ´Â ¹æ¹ýÀ» º¸ÀÌ°Ú´Ù.
Java monitor´Â re-entrantÀÌ´Ù.
2.6.4.1 µ¿±âÈ.
µ¿½Ã¿¡(concurrently) ½ÇÇàµÇ´Â thread´Â data¸¦ °øÀ¯Çϱâ À§Çؼ ¼·Î
´Ù¸¥ threadÀÇ »óÅÂ(state)¿Í Çൿ(activities)À» °í·ÁÇá¾ßÇÏ´Â Àç¹ÌÀÖ´Â »óȲ
ÀÌ ÀÚÁÖ ¹ßÇàÇÑ´Ù. ÀϹÝÀûÀ¸·Î »ý»êÀÚ/¼ÒºñÀÚ(Procuder/Consumer)ÀÇ ¿¹¸¦ ¸¹À̵é¾î
¼³¸íÇϵíÀÌ ÀÌ°÷¿¡¼µµ ±×·¯ÇÑ ¹æ½ÄÀ¸·Î ¼³¸íÇÏ°Ú´Ù.
¿¹¸¦ µé¾î¼, Java application¿¡¼ file¿¡ data¸¦ writeÇÏ´Â thread(producer)°¡
ÀÖ°í, file¿¡¼ data¸¦ ÀÐ¾î ¿À´Â thread(consumer)°¡ ÀÖ´Ù°í °¡Á¤ÇÏÀÚ.
¶Ç´Â, keyboard¿¡¼ key ÀÔ·ÂÀ» ±â´Ù¸±¶§ producer °¡ key event¸¦ queue¿¡ ¾²°í,
consumer´Â °°Àº queue¿¡¼ event¸¦ Àоî¿À´Â °æ¿ì°¡ ÀÖ´Ù.
ÀÌ ¸ðµç ¿¹´Â common resource¸¦ °øÀ¯ÇÏ´Â concurrent threadÀÌ´Ù.
±×¸®°í, ÀÌ thread´Â common resource¸¦ °øÀ¯Çϱ⠶§¹®¿¡, ¾î¶°ÇÑ ¹æ¹ýÀ¸·Îµç
µ¿±âÈ µÇ¾î¾ß ÇÑ´Ù.
Producer/Consumer Example
´ÙÀ½Àº 0~9°¡Áö ¼¼¸é¼ "CubbyHole" object¿¡ °ªÀ» ÀúÀåÇÏ°í, °ªÀ» Ãâ·ÂÇÏ´Â
ProducerÀÌ´Ù. ±×¸®°í, 0~100 millisecondµ¿¾È ºÒ±ÔÄ¢ÇÏ°Ô sleepÇÑ´Ù.
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
for (int i = 0; i < 10; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int)(Math.random() * 100));
} catch (InterruptedException e) {
}
}
}
}
´ÙÀ½Àº CubbyHole¿¡¼ ¸ðµç integer¸¦ ¼Ò¸ðÇÏ´Â ConsumerÀÌ´Ù.
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
int value = 0;
for (int i = 0; i < 10; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
À§ÀÇ ¿¹¿¡¼, Producer¿Í Consumer´Â "CubbyHole" object¸¦ ÅëÇؼ data¸¦
°øÀ¯ÇÑ´Ù. ±×·¯³ª, À§ÀÇ ¿¹¿¡¼ Producer°¡ °ªÀ» ¾²°í, Consumer°¡ °ªÀ»
Çϳª Àд ºÎºÐÀ» ¸í½ÃÇÑ ºÎºÐÀº, CubbyHole¿¡¼ get(), put() method¸¦ »ç¿ëÇÏ¿©
³»ºÎÀûÀ¸·Î Ç¥ÇöµÇ¾ú´Ù. ±×·¯³ª, ÁÖÀ§ÇÒÁ¡Àº ÀÌ µÎ threadÀÇ µ¿±âÈ¿¡ °üÇÑ ºÎºÐÀº
¾ø´Ù.
±×·¯¸é ¹ß»ýµÇ´Â ù¹ø° ¹®Á¦¸¦ °í·ÁÇغ»´Ù.
Producer°¡ Consumerº¸´Ù »¡¶ó¼ Producer°¡ 2°³¸¦ ó¸®Çߴµ¥, Cunsumer´Â
Çϳª¹Û¿¡ ó¸®ÇÏÁö ¸øÇؼ, Consumer´Â Çϳª¸¦ °Ç³Ê¶Ù°Ô µÇ¾ú´Ù.
µû¶ó¼ ´ÙÀ½°ú °°Àº °á°ú°¡ ³ª¿Ã ¼ö ÀÖ´Ù.
. . .
Consumer #1 got: 3
Producer #1 put: 4
Producer #1 put: 5
Consumer #1 got: 5
. . .
¶Ç´Ù¸¥ ¹®Á¦Á¡ÀÌ ¹ß»ýÇÒ ¼ö ÀÖ´Ù.
Consumer°¡ Producerº¸´Ù »¡¶ó¼ Cunsumer´Â °°Àº °ªÀ» µÎ¹ø Àо´Ù.
µû¶ó¼ ´ÙÀ½°ú °°Àº °á°ú°¡ ³ª¿Ã ¼ö ÀÖ´Ù.
. . .
Producer #1 put: 4
Consumer #1 got: 4
Consumer #1 got: 4
Producer #1 put: 5
. . .
À§ÀÇ µÎ°æ¿ì´Â ¸ðµÎ À߸øµÇ¾ú´Ù. ¿ì¸®´Â Producer°¡ Á¤È®È÷ Çϳª¸¦ writeÇϸé
Consumer´Â Á¤È®È÷ Çϳª¸¦ Àо±â¸¦ ¹Ù¶õ´Ù. À§¿Í °°ÀÌ, ÇϳªÀÇ object¸¦
»ç¿ëÇÏ¿© ¿©·¯°³ÀÇ thread°¡ µ¿½Ã¿¡ ºñµ¿±âÀû(asynchronously)À¸·Î ½ÇÇàµÇ¾î
À߸øµÈ °á°ú¸¦ ÃÊ·¡Çϴ°ÍÀ» "race condition"À̶ó°í ÇÑ´Ù.
À§¿Í Producer/Consumer¿¹Á¦¿¡¼ race conditionÀ» ¹æÁöÇϱâ À§ÇØ, Producer¿Í
Consumer´Â µ¿±âÈ(synchronized)°¡ µÇ¾î¾ß ÇÑ´Ù. ¶ÇÇÑ, CubbyHole°ú °°Àº
object´Â µ¿½Ã¿¡ µÎ°³ÀÇ thread¿¡ ÀÇÇؼ µ¿±âÈ µÇ¸é¼ Á¢±ÙµÇ´Âµ¥, ÀÌ·¯ÇÑ
object¸¦ "condition variable"À̶ó°í ÇÑ´Ù. Java language´Â 'monitors'¸¦
ÀÌ¿ëÇÏ¿© condition variableÀ» ÀÌ¿ëÇÏ´Â thread¸¦ µ¿±âÈ ½ÃŲ´Ù.
Monitors
Java language¿Í runtime systemÀº ¾àÀÚ·Î C.A.R±â´ÉÀ» ÇØÁÖ´Â 'monitors'¸¦
ÀÌ¿ëÇÏ¿© thread synchronizationÀ» Á¦°øÇÑ´Ù. ÀϹÝÀûÀ¸·Î, monitor´Â
Ưº°ÇÑ data item(condition variable)°ú, data¿¡ lockÀ» °Å´Â function°ú
°ü·ÃÀÌ ÀÖ´Ù. Thread°¡ ¾î¶² data itemÀ» À§ÇÑ monitor¸¦ °®°í ÀÖÀ»¶§,
´Ù¸¥ thread´Â data¸¦ ó¸®ÇÏ´Â ºÎºÐÀº lockµÇ¾î¼ Á¢±ÙÇÏÁö ¸øÇÑ´Ù.
°°Àº data¸¦ Á¢±ÙÇÏ´Â concurrent thread¸¦ ¼·Î ºÐ¸® Çϱâ À§ÇÑ program³»ºÎÀÇ
code segment¸¦ ÀӰ迵¿ª(critical section)À̶ó°í ÇÑ´Ù. Java language¿¡¼´Â,
'synchronized' keyword¸¦ »ç¿ëÇÏ¿© critical sectionÀ» ¼±¾ðÇÒ ¼ö ÀÖ´Ù.
ÀϹÝÀûÀ¸·Î, Java programÀÇ critical sectionÀº methodÀÌ´Ù. µ¿±âÈ ÇÒ¼ö ÀÖµµ·Ï
Á¶±×¸¸ code segment¸¦ ½ÃÇϴ°ÍÀÌ´Ù. ±×·¯³ª, ÀÌ°ÍÀº °´Ã¼ÁöÇâ ±â¹ý¿¡ ¾î±ß³ª´Â
°ÍÀÌ°í, code¸¦ debuggingÇÏ°í À¯Áö/º¸¼ö Çϴµ¥ ¾î·Á¿òÀ» ÁØ´Ù. Java programming
ÀüüÀûÀÎ Çü½Ä¿¡ µû¶ó¼, 'synchronized'´Â method level¿¡¼¸¸ »ç¿ëÇϴ°ÍÀÌ ÁÁ´Ù.
Java language¿¡¼ À¯ÀÏÇÑ monitor±â¹ýÀº 'synchronized'¸¦ »ç¿ëÇÑ ¹æ¹ýÀÌ´Ù.
´ÙÀ½Àº À§ÀÇ Producer/Consumer°¡ »ó¿äÇÑ CubbyHole classÀÌ´Ù. put() method´Â
CubbyHoleÀÇ °ªÀ» ¹Ù²Ù°í, get() method´Â °ªÀ» Àоî¿Â´Ù.
class CubbyHole {
private int seq;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
return seq;
}
public synchronized void put(int value) {
seq = value;
available = true;
notify();
}
}
¶ÇÇÑ, CubbyHoleÀº µÎ°³ÀÇ private variableÀ» °¡Áö°í ÀÖ´Ù.; seq´Â °ªÀ»
°¡Áö°í ÀÖ°í, 'available'Àº CubbyHoleÀÇ °ªÀÌ È¸¼öµÉ ¼ö ÀÖ´ÂÁö¸¦ ³ªÅ¸³»´Â boolean
°ªÀ» °¡Áö°í ÀÖ´Ù. availableÀÌ falseÀ̸é Producer´Â CubbyHole¿¡ »õ·Î¿î °ªÀ»
³Ö¾î¾ß ÇÏ°í, À̶§ Consummer´Â ¾ÆÁ÷ ±× °ªÀ» ¼Ò¸ð ÇÒ ¼ö ¾ø´Ù.
Consumer´Â ¿ÀÁ÷ availableÀÌ trueÀ϶§ °ªÀ» ¼Ò¸ð ÇÒ ¼ö ÀÖ´Ù.
CubbyHoleÀº synchronized method¸¦ °¡Áö°í Àֱ⠶§¹®¿¡, Java language´Â
Producer¿Í, ConsumerÀÇ ¿¹¸¦ Æ÷ÇÔÇÑ, °¢°¢ÀÇ CubbyHole instance¿¡ ¿ÀÁ÷ ÇϳªÀÇ
monitor¸¸À» ºÎ¿©ÇÑ´Ù. Producer°¡ CubbyHoleÀÇ pub() method¸¦ È£Ãâ ÇÒ ¶§¸¶´Ù,
Producer´Â CubbyHole·Î ºÎÅÍ monitor¸¦ ¾ò¾î³»¾ß¸¸ ÇÑ´Ù.
public synchronized void put(int value) {
// monitor has been acquired by the Producer
seq = value;
available = true;
notify();
// monitor is released by the Producer
}
put() method°¡ returnµÇ¸é, Produer´Â monitor¸¦ ³»³õÀ½À¸·Î½á CubbyHoleÀ»
lockÀ» Ç®¾îÁØ´Ù.
¸¶Âù°¡Áö·Î, Consumer°¡ CubbyHoleÀÇ get() method¸¦ È£Ãâ ÇÒ¶§¸¶´Ù,
Consumer´Â CubbyHole·Î ºÎÅÍ monitor¸¦ ¾ò¾î³»¾ß¸¸ ÇÑ´Ù.
public synchronized int get() {
// monitor has been acquired by the Consumer
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
return seq;
// monitor is released by the Consumer
}
Java runtime systemÀº monitor¸¦ ¾ò°í, ³»³õ´Â°ÍÀ» ÀÚµ¿À¸·Î ó¸®ÇÑ´Ù. ÀÌ°ÍÀº
¼öÇàÁßÀÇ thread°£ÀÇ race conditionÀ» ¹æÁöÇϸç, Á¤È®ÇÑ data¸¦ º¸ÀåÇØÁØ´Ù.
±×·¯³ª, CybbyHoleÀÇ put(), get() method¿¡ µû¸¥ ÀáÀçÀûÀÎ ¹®Á¦Á¡ÀÌ ÀÖ´Ù.
get() method¸¦ ½ÇÇà½Ãų¶§, ¸¸¾à CubbyHoleÀÇ °ªÀÌ ¾ø´Ù¸é, Consumer´Â Producer°¡
»õ·Î¿î °ªÀ» ½á³ÖÀ» ¶§±îÁö ±â´Ù¸±°ÍÀÌ´Ù.¿©±â¿¡¼ ¹®Á¦°¡ »ý±â´Âµ¥,
¸¸¾à CubbyHoleÀÌ monitor¸¦ Áã°í ÀÖ´Ù¸é, Producer´Â ¾î¶»°Ô »õ·Î¿î °ªÀ» ½á³ÖÀ»
¼ö ÀÖÀ»°ÍÀΰ¡?
Java language designer´Â ÀÌ°Í ¿ª½Ã »ý°¢ÇÏ°í ÀÖ´Ù. Thread°¡ wait() method¸¦
½ÇÇà ½ÃÅ°¸é, monitor´Â ÀÚµ¿À¸·Î ³»³õ°Ô µÈ´Ù. ±×¸®°í, wait() method¸¦ exitÇϸé,
´Ù½Ã monitor¸¦ ¾ò°Ô µÈ´Ù. ÀÌ°ÍÀº Producer°¡ monitor¸¦ ¾òÀ» ¼ö ÀÖ´Â ±âȸ¸¦
Á¦°øÇÏ°í CubbyHole¿¡ »õ·Î¿î °ªÀ» ³ÖÀ»¼ö ÀÖµµ·Ï ÇÑ´Ù.
The Main Program
´ÙÀ½Àº CubbyHole object, Producer, Consumer¸¦ »ý¼ºÇÏ°í, Producer¿Í
Cosumer¸¦ ½ÇÇà ½ÃÅ°´Â °£´ÜÇÑ Java applicationÀÌ´Ù.
class ProducerConsumerTest {
public static void main(String args[]) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
The Output
´ÙÀ½Àº ProducerConsumerTestÀÇ Ãâ·ÂÀÌ´Ù.
Producer #1 put: 0
Consumer #1 got: 0
Producer #1 put: 1
Consumer #1 got: 1
Producer #1 put: 2
Consumer #1 got: 2
Producer #1 put: 3
Consumer #1 got: 3
Producer #1 put: 4
Consumer #1 got: 4
Producer #1 put: 5
Consumer #1 got: 5
Producer #1 put: 6
Consumer #1 got: 6
Producer #1 put: 7
Consumer #1 got: 7
Producer #1 put: 8
Consumer #1 got: 8
Producer #1 put: 9
Consumer #1 got: 9
À§ÀÇ CubbyHole¿¡¼ ±½Àº ±Û¾¾¸¦ Á¦°ÅÇÏ°í ´Ù½Ã compileÇÏ°í, ½ÇÇà ½ÃÄѺ¸¸é
Producer¿Í Consumer threadÀÇ µ¿±âÈ¿¡ ÀüÇô ½Å°æÀ» ¾²Áö ¾Ê±â ¶§¹®¿¡,
´ç¿¬È÷ ¿À·ù°¡ »ý±ä´Ù.
2.6.4.2 Deadlock.
2.6.4.3 Re-entrant Monitors.
2.6.5 ¿ä¾à.
À§¿¡¼´Â ¿©·¯°¡Áö ¸¹Àº threadÀÇ »ó¿ä¹ý°ú, ´Ù¾çÇÑ ³»¿ë¿¡ ´ëÇؼ ¾Ë¾Æº¸¾Ò´Ù.
Thread´Â Java develpment environment¿¡¼ Á¦°øµÇ±â ¶§¹®¿¡, ÀϹÝÀûÀÎ ¸ð½À°ú´Â
È¥µ·ÀÌ ÀÖÀ» ¼ö ÀÖ´Ù. ´ÙÀ½Àº Java therad¸¦ Á»´õ Àß ¾Ë¼ö ÀÖµµ·Ï,
Java¿¡¼ Á¦°øµÇ´Â ´Ù¾çÇÑ class, method¿Í ¾ð¾îÀûÀÎ Ãø¸é¿¡ ´ëÇؼ ¿ä¾àÇغ¸°Ú´Ù.
package Support of Thread
java.lang.Thread
Java development environment¿¡¼, thread´Â java.langÀÇ Thread class¿¡¼
ÆÄ»ýµÈ objectÀÌ´Ù. Thread class´Â Java thread·Î ¼öÇàµÈ´Ù. ÀڽŸ¸ÀÇ
Runnable interface³ª thread¸¦ Á¦°øÇϱâ À§Çؼ Thread classÀÇ subclass¸¦
Á¦ÀÛÇÒ ¼ö ÀÖ´Ù.
java.lang.Runnable
Java language library´Â ¶ÇÇÑ ¾î¶°ÇÑ object°Ç °£¿¡ run() method¸¦ »ç¿ëÇÒ ¼ö
ÀÖµµ·Ï Thread¸¦ À§ÇÑ Runnable interface¸¦ Á¦°øÇÑ´Ù.
java.lang.Object
°¡Àå ±âº»ÀÌ µÇ´Â Object class´Â condition variable¿¡ µû¸¥ wait(), notify(),
notifyAll() ¿Í°°Àº synchronized method¸¦ Á¦°øÇÑ´Ù.
java.lang.ThreadGroup
¸ðµç thread´Â ÀϹÝÀûÀ¸·Î °ü·ÃµÈ threadµé ³¢¸®, thread groupÀ» Çü¼ºÇÑ´Ù.
java.lang package¿¡ ÀÖ´Â ThreadGroup class´Â ÀÌ·¯ÇÑ thread groupÀ»
Çü¼ºÇÒ ¼ö ÀÖµµ·Ï ó¸®ÇØÁØ´Ù.
java.lang.Death
Thread´Â ÀϹÝÀûÀ¸·Î ThreadDeath object¿¡ ÀÇÇؼ kill µÈ´Ù. ÀÌ·¸°Ô
ThradDeath°¡ catchµÇ¾î¼ ½ÇÁúÀûÀ¸·Î Á×À» ¼ö ÀÖ´Â thread´Â µå¹°´Ù.
2.7 ¿À·ù ó¸®¿Í ¿¹¿Ü »óȲ.
2.8 ÀÔÃâ·Â Stream.