Therad.join : 主要會完成一個程式後再跳出.
public class RunnableTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CRTest AAA = new CRTest("AAA");
CRTest BBB = new CRTest("BBB");
Thread t1 = new Thread(AAA);
Thread t2 = new Thread(BBB);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("end main");
}
}
public class CRTest implements Runnable{
private String id;
public CRTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
int time = (int)(1000*Math.random());
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing... Time : " + time);
}
}
}
=====================================
AAA is runing... Time : 376
AAA is runing... Time : 46
AAA is runing... Time : 414
AAA is runing... Time : 438
BBB is runing... Time : 158
BBB is runing... Time : 801
BBB is runing... Time : 373
BBB is runing... Time : 864
end main
=====================================
2012年8月24日 星期五
sleep + random
public class CRTest implements Runnable{
private String id;
public CRTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
int time = (int)(1000*Math.random());
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing... Time : " + time);
}
}
}
public class RunnableTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CRTest AAA = new CRTest("AAA");
CRTest BBB = new CRTest("BBB");
Thread t1 = new Thread(AAA);
Thread t2 = new Thread(BBB);
t1.start();
t2.start();
}
}
=======================================
AAA is runing... Time : 313
BBB is runing... Time : 671
AAA is runing... Time : 539
AAA is runing... Time : 239
AAA is runing... Time : 173
BBB is runing... Time : 628
BBB is runing... Time : 25
BBB is runing... Time : 762
========================================
Thread & Runnable
Thread
public class ThreadTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CTest AAA = new CTest("AAA");
CTest BBB = new CTest("BBB");
AAA.start();
BBB.start();
}
}
public class CTest extends Thread{
private String id;
public CTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing...");
}
}
}
=================================================
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
=================================================
Runnable
public class RunnableTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CRTest AAA = new CRTest("AAA");
CRTest BBB = new CRTest("BBB");
Thread t1 = new Thread(AAA);
Thread t2 = new Thread(BBB);
t1.start();
t2.start();
}
}
public class CRTest implements Runnable{
private String id;
public CRTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing...");
}
}
}
===================================
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
====================================
public class ThreadTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CTest AAA = new CTest("AAA");
CTest BBB = new CTest("BBB");
AAA.start();
BBB.start();
}
}
public class CTest extends Thread{
private String id;
public CTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing...");
}
}
}
=================================================
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
=================================================
Runnable
public class RunnableTest{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CRTest AAA = new CRTest("AAA");
CRTest BBB = new CRTest("BBB");
Thread t1 = new Thread(AAA);
Thread t2 = new Thread(BBB);
t1.start();
t2.start();
}
}
public class CRTest implements Runnable{
private String id;
public CRTest(String s){
id = s;
}
public void run() {
for(int i = 0; i < 4; i++){
for(int j = 0; j < 10000000; j++);
System.out.println(id + " is runing...");
}
}
}
===================================
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
AAA is runing...
BBB is runing...
====================================
2012年8月22日 星期三
IO Writer & Read
package kao.chih.chien.test;
import java.io.FileWriter;
import java.io.IOException;
public class WriterTest {
static String WRITER_TEST_PATH = "C:\\Test.txt";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileWriter fw = new FileWriter(WRITER_TEST_PATH,true);
fw.write("\tHello");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package kao.chih.chien.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadTest {
static String READ_TEST_PATH = "C:\\Test.txt";
static char data[] = new char[1024];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader(READ_TEST_PATH);
int num = fr.read(data);
String s = new String(data, 0, num);
System.out.println(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileWriter;
import java.io.IOException;
public class WriterTest {
static String WRITER_TEST_PATH = "C:\\Test.txt";
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileWriter fw = new FileWriter(WRITER_TEST_PATH,true);
fw.write("\tHello");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package kao.chih.chien.test;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadTest {
static String READ_TEST_PATH = "C:\\Test.txt";
static char data[] = new char[1024];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader(READ_TEST_PATH);
int num = fr.read(data);
String s = new String(data, 0, num);
System.out.println(s);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2012年8月8日 星期三
JAVA CLASS 基本架構 & 建立新的物件
建立class :
A. 建一個class需要 1. 變數 2.建構子 3.method方法.
B. class中不能在有其他class.
class 類別名稱
{
資料型態 field 名稱 ;
建構子 ()
{
...............
}
傳回值的資料型態 method 名稱 ( 引數 1, ............ )
{
}
}
建立新的物件 :
A. 宣告指向 "類別"的變數.
B. 利用new關鍵字,建立新的物件,並指派給所建立的變數.
Hello A;
A = new Hello();
也可以縮成一行
Hello A = new Hello();
2012年7月30日 星期一
JAVA 類別變數 & 類別方法
類別變數
類別變數可以解釋為共同變數,當一個程式用同一個class產生2個物件(A物件,B物件),當A物件改變了類別變數,B的類別變數也會跟著改變.
類別方法
類別方法是可以不用使用new一個物件,就可以使用類別方法.
class CCircle // 定義類別CCircle
{
private int num=0; // 設定num為「類別變數」
private static double pi=3.14; // 設定pi為「類別變數」
private double radius;
public CCircle(double r){ // CCircle建構元
radius=r;
num++; // 當CCircle()建構元被呼叫時,num便加1
}
public void show(){
System.out.println("area="+pi* radius*radius);
}
public static void count(){ // count() method,用來顯示目前物件建立的個數 「 類別方法 」
System.out.println(num+" object(s) created");
}
}
public class app8_8
{
public static void main(String args[])
{
CCircle.count();
CCircle cir1=new CCircle(1.0);
cir1.count(); // 用cir1物件呼叫count() method
CCircle cir2=new CCircle(2.0);
cir1.count(); // 用cir1物件呼叫count() method
cir2.count(); // 改用cir2物件呼叫count() method
System.out.println("Math.abs(- 9.9)= "+Math.abs(-9.9));
}
}
{
private int num=0; // 設定num為「類別變數」
private static double pi=3.14; // 設定pi為「類別變數」
private double radius;
public CCircle(double r){ // CCircle建構元
radius=r;
num++; // 當CCircle()建構元被呼叫時,num便加1
}
public void show(){
System.out.println("area="+pi*
}
public static void count(){ // count() method,用來顯示目前物件建立的個數 「 類別方法 」
System.out.println(num+" object(s) created");
}
}
public class app8_8
{
public static void main(String args[])
{
CCircle.count();
CCircle cir1=new CCircle(1.0);
cir1.count(); // 用cir1物件呼叫count() method
CCircle cir2=new CCircle(2.0);
cir1.count(); // 用cir1物件呼叫count() method
cir2.count(); // 改用cir2物件呼叫count() method
System.out.println("Math.abs(-
}
}
2012年7月13日 星期五
JAVA 建盤輸入
import java.io.*;
public class app2_1
{
public static void main(String args[]) throws IOException
{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入數字 ");
String S1 = b.readLine();
System.out.println("輸入數字為 " + S1);
}
}
java.io
Class BufferedReader
java.lang.Objectjava.io.Reader
java.io.BufferedReader
- Direct Known Subclasses:
- LineNumberReader
StringreadLine()
Read a line of text.readLine
public String readLine() throws IOException
- Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
-
- Returns:
- A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
- Throws:
IOException- If an I/O error occurs
2012年7月11日 星期三
JAVA 命名規則
變數或方法從小寫字母開始
EX : myVariable
類別從大寫字母開始
EX : ShirtTest
常數識別字都常使用大寫字母和分離的字母加底線
EX : SALES_TAX
EX : myVariable
類別從大寫字母開始
EX : ShirtTest
常數識別字都常使用大寫字母和分離的字母加底線
EX : SALES_TAX
JAVA 資料型態
分類
|
保留字
|
名稱
|
Byte
|
有效範圍
|
整
數
|
byte
|
位元組
|
1
|
-128~127
|
short
|
智整數
|
2
|
-32,768~32,767
| |
int
|
整數
|
4
|
-2,147,483,648~2,147,483,647
| |
long
|
長整數
|
8
|
-9,223,372,036,854,775,808~
9,223,672,036,854,775,807
| |
浮
點
|
float
|
浮點數
|
4
|
負值-3.402823E38~-1.401298E-45
正值1.401298E-45~3.402823E38
|
double
|
倍精數
|
8
|
負值-1.797693134E3.8~4.9406564584124E-324
正值4.94.6564584E-324~1.797693134862E308
| |
其
他
|
char
|
字元
|
2
|
\u0000~\Uffff
|
boolean
|
布林值
|
2
|
true,false
|
Integer.parseInt (字串轉整數)
Integer.parseInt : 為字串轉整數
java.lang
java.lang
Class Integer
java.lang.Objectjava.lang.Number
java.lang.Integer
- All Implemented Interfaces:
- Serializable, Comparable<Integer>
Method Summary
Example:
public class app1_4
{
public static void main(String args[])
{
int n1 = Integer.parseInt(args[0]);
int n2 = Integer.parseInt(args[1]);
System.out.println("n1 + n2 = " + (n1 + n2));
}
}
訂閱:
意見 (Atom)














