1113线程.docx
- 文档编号:5543490
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:17
- 大小:17.19KB
1113线程.docx
《1113线程.docx》由会员分享,可在线阅读,更多相关《1113线程.docx(17页珍藏版)》请在冰点文库上搜索。
1113线程
一.多线程
1.一个线程用来解决一个业务逻辑,多个线程用来解决多个业务逻辑。
2.进程与线程的区别
A、进程:
程序执行的流程,范畴属于操作系统,一个进程占有独立内存的;进程可以叫一个大级别的线程(进程想当一个操作系统)
B、线程:
应用程序,共享一个内存;线程可以叫一个小级别的进程。
3.的线程就是所谓流程
4.怎么能实现多线程?
有两个途径:
创建Thread类的子类或实现Runnable接口。
A、用Thread类的子类创建线程
//多线程
classExample1
{
staticLefthandleft;staticRighthandright;
publicstaticvoidmain(Stringargs[])
{
left=newLefthand();//创建两个线程
right=newRighthand();
left.start();
right.start();
}
}
classLefthandextendsThread
{
publicvoidrun()
{
for(inti=0;i<=5;i++)
{
System.out.println("Iamastudent");
try
{
sleep(500);
}
catch(InterruptedExceptione)
{
}
}
}
}
classRighthandextendsThread
{
publicvoidrun()
{
for(inti=0;i<=5;i++)
{
System.out.println("Iamopoookkkkk");
try
{
sleep(500);
}
catch(InterruptedExceptione)
{
}
}
}
}
5.线程生命周期
A.new新建
B.start启动/就绪
C.run()运行线程处于自动调用业务逻辑
D.sleep阻塞
E.死亡方法执行完毕实现run()方法
6.调用机制:
同级别的采用先到服务的原则,线程的级别继承父线程(程序的主线程
)主线程一般具有普通优先级
不同级别的按照优先级的顺序
//实现Runnable接口的实例
importjava.applet.*;
importjava.awt.*;
publicclassExample2extendsjava.applet.AppletimplementsRunnable
{
ThreadcircleThread;
publicvoidstart()
{
if(circleThread==null)
{
circleThread=newThread(this);
circleThread.start();
}
}
publicvoidrun()
{
while(circleThread!
=null)
{
repaint();
try
{
circleThread.sleep(1000);
}
catch(InterruptedExceptione)
{
}
}
}
publicvoidpaint(Graphicsg)
{
doublei=Math.random();
if(i<0.5)
{
g.setColor(Color.red);
}
else{
g.setColor(Color.black);
}
g.fillOval(100,100,(int)(100*i),(int)(100*i));
}
publicvoidstop()
{
circleThread.yield();
circleThread=null;
}
publicstaticvoidmain(Stringargs[])
{
newExample2();
}
}
//是一个应用程序,这个应用程序在创建窗口的同时有创建一个新的线程,该程序负责让窗口只能够的一个按钮改变他的大小
importjava.awt.event.*;
importjava.awt.*;
publicclassExample3
{
publicstaticvoidmain(Stringargs[])
{
Mywinwin=newMywin();
}
}
classMywinextendsFrameimplementsRunnable
{
Buttonb=newButton("ok");
intx=5;
Threadbird=null;
Mywin()
{
setBounds(100,100,120,120);
setLayout(newFlowLayout());
setVisible(true);
add(b);
b.setBackground(Color.green);
bird=newThread(this);
bird.start();
}
publicvoidrun()
{
while(true)
{
x=x+1;
if(x>100)x=5;
b.setBounds(40,40,x,x);
try
{
bird.sleep(200);
}
catch(InterruptedExceptione){
}
}
}
}
//滚动字幕线程
importjava.applet.*;
importjava.awt.*;
publicclassExample4extendsjava.applet.AppletimplementsRunnable
{
intx=0;ThreadScrollwords=null;
publicvoidinit()
{
setBackground(Color.cyan);
setForeground(Color.red);
setFont(newFont("TimesRman",Font.BOLD,18));
}
publicvoidstart()
{
if(Scrollwords!
=null)
{
Scrollwords=newThread(this);
Scrollwords.start();
}
}
publicvoidrun()
{
while(Scrollwords!
=null)
{
x=x+5;
if(x>500)
x=0;
repaint();
try{Scrollwords.sleep(80);
}
catch(InterruptedExceptione)
{
}
}
}
publicvoidstop()
{
Scrollwords.yield();
Scrollwords=null;
}
publicstaticvoidmain(Stringargs[])
{
newExample4();
}
}
//带滚动字符的小字典
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassExample5extendsAppletimplementsActionListener,Runnable
{
TextFieldtext1,text2;
intx=0;
ThreadScrollwords=null;
publicvodiinit()
{
setBackground(Color.cyan);
setForeground(Color.red);
setFont(newFont("TimesRman",Font.BOLD,18));
text1=newTextField(10);
text2=newTextField(10);
add(newLabel("输入一个英文单词:
"));
add(text1);
add(newLabel("汉语意思"));
add(text2);
text1.addActionListener(this);
}
publicvoidstart()
{
if(Scrollwords!
=null)
{
Scrollwords=newThread(this);
Scrollwords.start();
}
}
publicvoidrun()
{
while(Scrollwords!
=null)
{
x=x+5;
if(x>500)
x=0;
repaint();
try{Scrollwords.sleep(80);
}
catch(InterruptedExceptione)
{
}
}
}
publicvoidpaint(Graphicsg)
{
g.drawString("欢迎使用字典",x,120));
}
publicvoidstop()
{
Scrollwords.yield();
Scrollwords=null;
}
publicvoidactionPerformed(ActionEvente)
{
if((e.getSource()==text1)&&(text1.getText().equals("boy")))
{
text2.setText("男孩");
}
elseif(e.getSource()==text1)&&(text1.getText().equals("sun")))
{
text2.setText("太阳");
}
else
{
text2.setText("没有该单词");
}
}
publicstaticvoidmain(Stringargs[])
{
newExample5();
}
}
//下面是一个左手画圆右手画方的列子,
//我吗在主线程中创建了两个线程,:
left,right,其中一个负责画圆,另一个画方。
在这个列子中我们使用了容器类的方法getGraphics()来获取一个Graphics对象(可以理解一个画笔)
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassExample6extendsAppletimplementsRunnable
{
Threadleft,right;Graphicsmypen;intx,y;
publicvoidinit()
{
left=newThread(this);
right=newThread(this);
x=10;y=10;
mypen=getGraphics();
}
publicvoidstart()
{
left.start();
right.start();
}
publicvoidrun()
{
while(true)
if(Thread.currentThread()==left)
{
x=x+1;
if(x>240)x=10;
mypen.setColor(Color.blue);
mypen.clearRect(10,10,300,100);
mypen.fillRect(10+x,10,50,50);
try{left.sleep(60);
}
catch(InterruptedExceptione)
{
}
}
elseif(Thread.currentThread()==right)
{
y=y+1;
if(y>240)y=10;
mypen.setColor(Color.red);
mypen.clearRect(10,10,300,100);
mypen.drawOval(10+y,110,50,50);
try{right.sleep(60);
}
catch(InterruptedExceptione)
{
}
}
}
publicvoidstop()
{
left=null;
right=null;
}
}
//下面是一个模拟平抛运动,一个求自由落下一个求同时水平抛出,二者同时落地
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassExample7extendsAppletimplementsRunnable
{
Threadredball,blueball;
GraphicsredPen,bluePen;
intt=0;
publicvoidinit()
{
redball=newThread(this);
blueball=newThread(this);
redPen=getGraphics();
bluePen=getGraphics();
}
publicvoidstart()
{
redball.start();
blueball.start();
}
publicvoidrun()
{
while(true)
{
t=t+1;
if(Thread.currentThread()==redball)
{
if(t>100)t=0;
redPen.clearRect(0,0,110,400);
bluePen.fillOval(50,(int)(1.0/2*t*9.8),50,50);
try{redball.sleep(40);
}
catch(InterruptedExceptione){
}
}
elseif(Thread.currentThread()==blueball)
{
bluePen.clearRect(120,0,900,500);
bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),50,50);
try{blueball.sleep(40);
}
catch(InterruptedExceptione){
}
}
}
}
}
//点击按钮后,每隔半秒中后就发出嘟的一声
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassExamples8extendsAppletimplementsActionListener
{
Toolkittoolkit;
Buttonbutton;
publicvoidinit()
{
toolkit=getToolkit();
button=newButton("确定");
add(button);
button.addActionListener(this);
}
publicvoidactionPerformed(ActionEventew)
{
if(ew.getSource()==button)
{
for(inti=0;i<=9;i++)
{
toolkit.beep();
try
{
Thread.sleep(500);
}
catch(InterruptedExceptione)
{
}
}
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 1113 线程
![提示](https://static.bingdoc.com/images/bang_tan.gif)