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
=====================================
JAVA
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日 星期一
訂閱:
意見 (Atom)












