Java进程

来源:互联网 发布:架子鼓谱软件 编辑:程序博客网 时间:2024/06/02 08:49

获取键盘上的输入流<键盘输入>

package com.fs.demo;

 

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintStream;

 

public class Test1 {

public static void main(String[] args) throws IOException {

PrintStream p = System.out;

p.println("sdjgla");

InputStream is = System.in;

byte[] b = new byte[100];

is.read(b, 0, b.length);

String str = new String(b);

System.out.println(str);

}

}

 

创建线程的其中三种方法

package com.fs.test;import java.util.Timer;import java.util.TimerTask;/** *  * @author 创建线程的其中三种方法 * 1、定义一个线程类,它继承Thread,并重写其中的方法run(),方法run()称为线程体;启动一个线程使用start()方法。 *  由于Java只支持单继承,使用这种方法定义的类不能再继承其它类。 *  * 2、提供一个实现接口Runnable的类作为线程的目标对象,首选这种方法创建线程,并重写其中的方法run(),用Thread类带参数(Runnable实现类),再调用start()方法实现。 *  * 3、定义一个线程类,它继承TimerTask,并重写其中的方法run(),用带参的schedule()方法开启子线程。<常用于定时器的使用> * *//* ==== 1、采用继承Thread的方式开线程,重写run方法,用Start方法(eg:     new MyThread().start()  )开启线程 ==== *///class MyThread extends Thread {//@Override//public void run() {//for (int i = 0; i < 20; i++) {//System.out.println("i="+i);//try {//Thread.sleep(1000);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}//}//}////public class Test3 {//public static void main(String[] args) {//new MyThread().start();//for (int i = 0; i < 20; i++) {//System.out.println("main i="+i);//try {//Thread.sleep(1000);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}//}//}/* ==== 2、采用继承Runnable的方式开线程,重写run方法,用Thread(带参).start()的方法(eg:     new Thread(new myRunnable()).start()  )开启线程 ==== *///class myRunnable implements Runnable {////@Override//public void run() {//for (int i = 0; i < 20; i++) {//System.out.println("myRunnable i = "+i);//try {//Thread.sleep(1000);//} catch (InterruptedException e) {//// TODO Auto-generated catch block//e.printStackTrace();//}//}//}////}////public class Test3 {//public static void main(String[] args) {//Runnable r = new myRunnable();//Thread t = new Thread(r);//t.start();////new Thread(new myRunnable()).start();//}//}/* ==== 3、采用继承TimerTask的方式开线程,重写run方法,用带参的schedule()方法(eg:     Timer timer = new Timer();timer.schedule(new myTimer(), 3000);  )开启线程 ==== */class myTimer extends TimerTask {public void run() {for (int i = 0; i < 10; i++) {System.out.println("i=" + i);try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}public class Test3 {// 定时器public static void main(String[] args) {Timer timer = new Timer();//3000ms后开子线程,在子线程中调用一次run方法timer.schedule(new myTimer(), 3000);// 5000ms后开子线程,在子线程重复调用run方法,间隔时间100ms调用一次timer.schedule(new myTimer(), 5000, 100);// <先结束上一个子进程后再开启这个子进程>try {Thread.sleep(24000);timer.cancel();//结束timer对象开避的子线程} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//new Timer().schedule(new myTimer(), 3000);// 进程不死。。。。。。}}//class myTimer extends TimerTask {//private int i = 0;//private Timer timer;//public myTimer(Timer timer) {//this.timer = timer;//}//public void run() {//i++;//if(i==20) {//timer.cancel();// 终止此计时器,丢弃所有当前已安排的任务//}//System.out.println("i="+i);//}//}////public class Test3 {//public static void main(String[] args) {//Timer timer = new Timer();//// 2000ms后开子线程,在子线程重复调用run函数,间隔时间100ms//timer.schedule(new myTimer(timer), 2000, 100);//}//}


 

0 0
原创粉丝点击