`
H小阿飞
  • 浏览: 274949 次
  • 性别: Icon_minigender_1
  • 来自: 南通
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
关于等待其他线程结束:jion()的疑问 (Page:381) thread
package main.thread;

public class Machine1 implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int a=0; a<10; a++){
			System.out.println(Thread.currentThread().getName()+":"+a);
		}
	}

	public static void main(String[] args) throws InterruptedException{
		Machine1 machine1 = new Machine1();
		Thread t = new Thread(machine1);
		t.setName("m1");
		t.start();
		System.out.println("main:join machine");
		t.join();         //主线程等待machine1线程运行结束
		machine1.run();   //machine1线程运行完后,主线程恢复运行
//		t.run();         //此方法,居然也是运行的主线程,为什么,不解??????
		System.out.println("main:end");
	}
}

注:此上问题,可以注释t.join();machine1.run();两行后,释放注释t.run()运行后,看运行结果
线程的实现 thread
 1.继承Thread类

package main.thread;

public class Machine extends Thread{
	
	public void run(){
		for(int a=0; a<10; a++){
			System.out.println(currentThread().getName()+":"+a);
			try{
				sleep(100);      //给其他线程运行的机会,这样machine1与machine2线程属于分时调度模式
			}catch(InterruptedException e){
				throw new RuntimeException(e);
			}
		}
	}
	
	public static void main(String[] args){
		System.out.println(currentThread().getName());  //该主线程
		Machine machine1 = new Machine();
		Machine machine2 = new Machine();		
		machine1.start();		//启动第一个Machine线程
		machine2.start();	    //启动第二个Machine线程
	}
}

  2.实现Runable接口
  Java不允许一个类继承多个类,因此一旦一个类继承了Thread类,就不能再继承其他的类。为了解决这一问题,Java提供了java.lang.Runnale接口,它有一个run()方法,定义如下:
   public void run();

package main.thread;

public class Machine implements Runnable{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int a=0; a<10; a++){
		System.out.println(Thread.currentThread().getName()+":"+a);
		try{
			Thread.sleep(100);
		}catch(InterruptedException e){
			throw new RuntimeException(e);
		}
	  }
	}

	public static void main(String[] args){
		Machine machine = new Machine();
		Thread t1 = new Thread(machine);
		Thread t2 = new Thread(machine);
		t1.start();
		t2.start();
	}
}

注:在Thread类中定义了如下形式的构造方法:
Thread(Runnable runnable)    //当线程启动时,讲执行参数runnable所引用对象的run()方法
继承与接口
package main;

public abstract class A {

	public abstract void test1();
	 
	public void test2(){
		System.out.println("我是父类中的具体方法,可以被我的子类直接继承");
	}
	
}

package main;

public interface C {

	public abstract void test3();
	
//	public void test3();  //合法,test3()默认为abstract

//	void test3();    //合法,test3()默认为public、abstract
}

package main;

public class B extends A implements C{

	static{
		System.out.println("我就是B!");
	} 

	@Override
	public void test1() {
		// TODO Auto-generated method stub
		System.out.println("我是实现父类B的test1方法");
	}

//	public void test2(){
//		System.out.println("我是覆写父类B的test2方法");
//	}
	
	@Override
	public void test3() {
		// TODO Auto-generated method stub
		System.out.println("我是实现接口C中test3方法");
	}
	
	public static void main(String[] args){
		B b = new B();
	    b.test1();
		b.test2();      //直接继承父类B中的方法
		b.test3();
	}
}

break与continue的区别
package main;

/**
 * 
 * @author 阿飞
 * 1.break语句用于终止某个语句块的执行,用在循环语句体中,可以强行退出循环
 * 2.continue语句用在循环语句体中,用于终止某此循环过程,跳过循环体中continue
 * 语句下面未执行的循环,开始下一次循环过程。
 *
 */
public class Break_Continue {

	public void Break(){
		int stop = 4;
		for(int i=0; i<6; i++){
			if(i==stop){
				break;
			}
		    System.out.println(i);
		}
	}
	
	public void Continue(){
		int skip = 4;
		for(int i=0; i<6; i++){
			if(i==skip){
				continue;
			}
		    System.out.println(i);
		}
	}
	
	public static void main(String[] args){
		Break_Continue bc = new Break_Continue();
		bc.Break();
		bc.Continue();
	}
}
数据类型转换基础
package main;

/*
数据类型           大小         范围                                     默认值 

byte(字节) 	   8      -128 - 127                                        0
shot(短整型)       16      -32768 - 32768                                   0
int(整型)          32   -2147483648-2147483648                              0
long(长整型)       64   -9233372036854477808-9233372036854477808            0        
float(浮点型)      32  -3.40292347E+38-3.40292347E+38                      0.0f
double(双精度)	   64  -1.79769313486231570E+308-1.79769313486231570E+308  0.0d
char(字符型)       16     ‘ \u0000 - u\ffff ’                          ‘\u0000 ’
boolean(布尔型)    1       true/false                                      false
*/
/**
 *
 * @author 阿飞
 * 
 * 1.容量小的类型自动转换为容量大的数据类型,数据类型按容量大小排序为:
 *   byte,short,char->int->long->float->double
 *   byte,short,char之间不会互相转换,他们三者在计算时首先会转换为int类型
 * 2.容量大的数据类型转换为容量小的数据类型时,要加上强制转换符,但可能
 *   造成精度降低或溢出,使用时要格外注意。
 * 3.有多种类型的数据混合运算时,系统首先自动的将所有数据转换成容量最大
 *   的那一种数据类型,然后再进行计算。
 *
 */
public class ConvertTester {

	public static void main(String[] args){
        int i = 2147483647; //int类型可以表达的最大数值为2的31次方然后再减1,也就是2147483647,大于此数值了,则报错。
		long l3 = 2147483647;
		long l4 = 2147483648L;  //java中看到整数类型都默认为int型,而2147483648超过了int型的表示范围,所以强制转换为long型。
		int i1 = 123;
		int i2 = 456;
		double d1 = (i1+i2)*1.2;  //系统将转换为double型运算 ,因为1.2是double型
		float f1 = (float)((i1+i2)*1.2);  //系统先将转换为double型运算,然后强制转换为float型
		byte b1 = 1;   //1为int类型,但是还是可以赋值给byte类型的b1,是因为1在byte表示范围数内,如果是128,则不可以赋值给b1
		byte b2 = 2;
		byte b3 = (byte)(b1+b2);  //系统将先转换为int型运算,然后再强制转换为byte型
		
		byte b4 = 67;
		byte b5 = 123;
		byte b6 = (byte)(b4+b5);   //===================此处有疑问,如下:
		System.out.println(b6);  //运算后的数值不溢出,会自动删去三个字节,但是是如果得到最后结果的,尚未自己运算起来。
		
		double d2 = 1e200;
		float f2 = (float)d2;   //会产生溢出,因为d2所表示的数值超过了float所表示的数值范围
		System.out.println(f2);  //打印出来的f2是Infinity,则是一个表示趋于无穷大的数值,因为超过了float的表示的数值范围
		
		float f3 = 1.23f;  //必须加f
		long l1 = 123;
		long l2 = 30000000000L;  //必须加L
		float f = l1+l2+f3;   //系统将会转换为float型计算
		long l = (long)f;   //强制转换会舍去小数部分(不是四舍五入)
	}
}

线程基础入门 thread
package thread;

/**
 * 用Thread类创建线程
 * @author 阿飞
 *
 */
public class ThreadTest1 extends Thread{

	public void run(){
		System.out.println("==========");
	}
	
	public static void main(String[] args){	
		while(true){
			ThreadTest1 t = new ThreadTest1();
			t.start();
			try{
				Thread.sleep(1000);
			}catch(Exception e){
				e.printStackTrace();
			}
			
		}	
	}
	
}


package thread;

/**
 * 使用Runnable接口创建线程
 * @author 阿飞
 *
 */
public class ThreadTest2 {
    
	public static void main(String[] args){
		TestRunnable tr = new TestRunnable();
		Thread t = new Thread(new TestRunnable());
		t.start();
	}
	
	
}
   class TestRunnable implements Runnable {

	@Override
	public void run() {
		while (true) {
			try {
				Thread.currentThread().sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("============");
			
		}

	}
}
图片的缩放(固定大小,大图片缩小后不失真) image
package test;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class Test {

	public static void reduceImg(String imgsrc, String imgdist, int widthdist,   
	        int heightdist) {   
	    try {   
	        File srcfile = new File(imgsrc);   
	        if (!srcfile.exists()) {   
	            return;   
	        }   
	        Image src = javax.imageio.ImageIO.read(srcfile);   
	  
	        BufferedImage tag= new BufferedImage((int) widthdist, (int) heightdist,   
	                BufferedImage.TYPE_INT_RGB);   
	  
	        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,  Image.SCALE_SMOOTH), 0, 0,  null);   
//	        tag.getGraphics().drawImage(src.getScaledInstance(widthdist, heightdist,  Image.SCALE_AREA_AVERAGING), 0, 0,  null);   
	           
	        FileOutputStream out = new FileOutputStream(imgdist);   
	        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
	        encoder.encode(tag);   
	        out.close();   
	  
	    } catch (IOException ex) {   
	        ex.printStackTrace();   
	    }   
	}
	
	public static void main(String[] args){
		reduceImg("D://1.jpg","D://test2.jpg",180,180);
	}
}
Socket通信基础 socket
package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

	private ServerSocket serverSocket;
	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	
	public Server(){
		try{
			serverSocket = new ServerSocket(10000);	
			while(true){
				socket = serverSocket.accept();
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out = new PrintWriter(socket.getOutputStream(),true);
				String line = in.readLine();
				System.out.println("you input is: "+line);
				out.close();
				in.close();
				socket.close();
//				serverSocket.close();
			}
			
		}catch(IOException e){
			
		}
	}
	
	public static void main(String[] args){
		new Server();
	}
}


package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {

	private Socket socket;
	private BufferedReader in;
	private PrintWriter out;
	
	public Client(){
		try{
			socket = new Socket("127.0.0.1",10000);
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(),true);
			BufferedReader line = new BufferedReader(new InputStreamReader(System.in));
			out.write(line.readLine());
			out.close();
			in.close();
			socket.close();
		}catch(IOException e){
			
		}
		
	}
	
	public static void main(String[] args){
		new Client();
	}
}
Global site tag (gtag.js) - Google Analytics