五:多线程
进程和线程
在多个任务系统中,每个独立执行的程序称为进程
一个进程中包含一个或多个线程,一个线程就是一个程序内部的一条执行线索,如果要一程序中实现多段代码同时交替运行,就需要产生多个线程,并指定每个线程上所要的程序代码段---多线程。
Class ThreadDemo1
{
Public static void main(String [] args)
New TestThread().start();
While(true)
{
System.out.println(“main()”+Thread.currentThread().getName());
}
}
}
Class TestThread extends Thread
{
Public void run()
{
While(true)
{
System.out.println(“run()”+Thread.currentThread().getName());
}
}
}
用Thread类创建线程
要将一段代码在一个新的线程上运行,该代码应该在一个类的run函数中,并且run函数所在的类是Thread类的子类。我们要实现多线程,必须编写一个继承了Thread类的子类,子类要覆盖Thread类中的run函数,在子类的run函数中调用想在新的线程上运行的程序代码。
启动一个新的线程,不是直接调用Thread的子类对象的run方法,而是调用Thread子类对象的start方法,Thread类的start方法将产生一个新的线程,并在该线程导航运行Thread类对象中的run方法,在该线程上实际运行的是Thread子类。
通过控制run方法中循环的条件来控制线程的结束
铁路售票
只能创建一个资源对象,不能用extends Thread
Class ThreadDemo1
{
Public static void main(String [] args)
Thread tt = new TestThread();
tt.start();
tt.start();
tt.start();
tt.start();
}
}
Class TestThread extends Thread
{
Int ticket = 100;
Public void run()
{
While(true)
{
If(ticket >0)
System.out.println(Thread.currentThread().getName()”is saling tacket”+tackets--);
}
}
}
--------------------------
Class ThreadDemo1
{Public static void main(String [] args)
Thread tt = new TestThread();
New Thread(tt).start();
New Thread(tt).start();
New Thread(tt).start();
New Thread(tt).start();
}
}
Class TestThread implement Runnable
{
Int ticket = 100;
Public void run()
{While(true)
{If(ticket >0){
System.out.println(Thread.currentThread().getName()”is saling tacket”+tackets--);
}
}
}
使用Runnable接口创建多线程
适合多个相同程序代码的线程去处理同一资源的情况,把虚拟CPU(线程)同程序的代码,数据有效分离。
可以避免由于java耳朵单继承特性带来的局限,当我们要将已经继承了某一个类的子类放入多线程中,由于一个类不能同时有两个父类,所以只能采用实现Runnable
当线程被构造时,需要的代码和数据通过一个对象作为构造函数实参传递进去,这个对象就是一个实现了Runnable接口的类的实例
所有多线程应用都可以用Runnable接口方式。
多线程同步
Class ThreadDemo1
{Public static void main(String [] args)
Thread tt = new TestThread();
New Thread(tt).start();
New Thread(tt).start();
New Thread(tt).start();
New Thread(tt).start();
}
}
Class TestThread implement Runnable
{
Int ticket = 100;
String str = new String(“”);
Public void run()
{While(true)
{
Synchronized(str)
{
If(ticket >0)
{
Try{Thread.Sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()”is saling tacket”+tackets--);
}
}
}
}
}
Java API
理解API:(Application Programming Interface)
Window API java API
Java工具软件 Jcreator
工作区jcw 工程jcp
String类和StringBuffer类
位于java.Jang 包中
String类对象中的内容一旦被初始化就不能再改变
StringBuffer类用于封装内容可以改变的字符串
用toString方法转换成String类型
String x = “a”+4+”c”编译时等效于
String x =new StringBuffer().append(“a”).append(4).append(“c”).toString();
字符串常量实际上是一种特殊的匿名String对象
String s1=”hello” == String s2=”hello” true
String s1=new String(”hello”) == String s2=new String(”hello”) false
String类的常用成员方法
构造方法
String(byte[] bytes,int offset,int length)
equalslgnoreCase方法 能判断大小写的数据
equals只能判断小写数据
indexOf(int ch)
返回一个字符在字符串首次位置,没有返回-1
Substring(int beginIndex)方法
Substring(int beginIndex.intendIndex)
返回从beginIndex到.intendIndex-1之间的字符
基本数据类型的对象包装类
集合类
用于存储一组对象,其中的每一个对象称为元素,经常会用到Vector,Enumeration,ArrayList,Collection,iterator,set,list等集合类和接口
Vector类和Enumeration接口
例:
将键盘上输入的一个数字序列中的每位数字存储在Vector对象中,然后再屏幕上打印出每位数字相加的结果
Import java.util.*:
Public class TestVector{
Public static void main(String[] args){
Int b= 0;
Vector v = new Vector();
System.out.println(“please enter number:”);
While(true)
{
Try
{
Int b = System.in.read();
}
Catch(Exception e)
{
e.printStackTrace();
}
If(b==’/r’||b==’/n’)
Break;
Else
{
Int num = b-‘0’;
v.addElement(new Integer(num));
}
}
Int sum = 0;
Enumeration e = v.elements();
While(e.hasMoreElements())
{
Integer intObj = (Integer)e.nextElement();//不是返回下一个对象,指示器指向的对象,指示器后移
Sum+=intObj.intValue();
}
System.out.println(sum);
}
}
Collection接口与Iterator接口
用ArrayList和Iterator改写上面的例子
Import java.util.*:
Public class TestCollection{
Public static void main(String[] args){
Int b= 0;
ArrayList v = new ArrayList ();
System.out.println(“please enter number:”);
While(true)
{
Try
{
Int b = System.in.read();
}
Catch(Exception e)
{
e.printStackTrace();
}
If(b==’/r’||b==’/n’)
Break;
Else
{
Int num = b-‘0’;
v.add (new Integer(num));
}
}
Int sum = 0;
Iterator e = v. iterator ();
While(e.hasNext())
{
Integer intObj = (Integer)e.next ();
Sum+=intObj.intValue();
}
System.out.println(sum);
}
}
Collection Set List区别
Collection 各元素对象之间没有指定的顺序,允许有重复元素和多个null元素对象
Set 各元素对象之间没有指定的顺序,不允许有重复元素,多个null元素对象
List 各元素对象之间有指定的顺序,允许有重复元素和多个null元素对象
Hashtable类
不仅可以像Vector一样动态存储一系列的对象,而且对存储的每一个对象都要安排另一个对象与之相关联
Properties类
是Hashtable的子类
增加了将Hashtable对象中的关键字和值保存到文件和从文件中读取关键字和值到Hashtable对象中的方法
如果要用Properties.store方法存储Properties对象中的内容,每个属性的关键字和值都必须是String类型
System类和Runtime类
System类
Exit方法 结束JVM运行