线程--demo3
示例1:SwingAndThread package com.etc.jichu;
import java.awt.Container;
import java.net.URL; import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class SwingAndThread extends JFrame
{
private JLabel jl=new JLabel();
private static Thread t;
private int count;
private Container container=new Container();
public SwingAndThread()
{
setBounds(300,200,250,100);
container.setLayout(null);
URL url=SwingAndThread.class.getResource("/004.jpg");//获取图片资源路径
Icon icon=new ImageIcon(url);//图标选择组件Icon
jl.setIcon(icon);
jl.setHorizontalAlignment(SwingConstants.LEFT);
jl.setBounds(10,10,200,50);
jl.setOpaque(true);
t=new Thread(new Runnable() {
public void run() {
while(count<=200)
{
jl.setBounds(count, 10, 200, 5);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
count+=4;//使横坐标每次增加4
if(count==200)
{
count=10;
}
} }
});
t.start();
container.add(jl);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args)
{
new SwingAndThread(); } }
示例2:SleepMethodTest package com.etc.jichu; import java.awt.Color;
import java.awt.Graphics;
import java.util.Random; import javax.swing.JFrame; public class SleepMethodTest extends JFrame
{
private Thread t;
private static Color[] color={Color.BLACK,Color.BLUE,Color.CYAN,Color.GREEN,Color.ORANGE,Color.YELLOW,Color.RED,Color.PINK,Color.LIGHT_GRAY};
private static final Random rand=new Random();
private static Color getC()//获取随机颜色值的方法
{
return color[rand.nextInt(color.length)];
}
public SleepMethodTest()
{
t=new Thread(new Runnable() {
int x=30;
int y=50;
public void run() {
while(true)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
Graphics graphics=getGraphics();
graphics.setColor(getC());
graphics.drawLine(x, y, 200, y++);
if(y>=300)
{
y=2800;
}
} }
});
t.start();
}
public static void init(JFrame frame,int width,int height)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void main(String[] args)
{
init(new SleepMethodTest(), 600, 600);
} }
示例3:jointest package com.etc.jichu; import java.awt.BorderLayout; import javax.swing.JFrame;
import javax.swing.JProgressBar; public class JoinTest extends JFrame
{
private Thread threadA;
private Thread threadB;
final JProgressBar progressBar=new JProgressBar();//进度条组件
final JProgressBar progressBar2=new JProgressBar();
int count =0;
public JoinTest() {
super();
getContentPane().add(progressBar,BorderLayout.NORTH);
getContentPane().add(progressBar2,BorderLayout.SOUTH);
progressBar.setStringPainted(true);
progressBar2.setStringPainted(true);
//匿名内部类方式实例化线程
threadA=new Thread(new Runnable()
{
int count=0;
public void run()
{
while(true)
{
progressBar.setValue(++count);
try {
Thread.sleep(100);
threadB.join();
} catch (Exception e) {
e.printStackTrace();
}
} }
});
threadA.start();
threadB=new Thread(new Runnable() {
int count=0;
public void run() {
while(true)
{
progressBar2.setValue(++count);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(count==100)
break;
} }
});
threadB.start();
}
public static void init(JFrame frame,int width,int height)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void main(String[] args)
{
init(new JoinTest(), 200, 200); } }
示例4:线程中断 package com.etc.jichu; import java.awt.BorderLayout; import javax.swing.JFrame;
import javax.swing.JProgressBar; public class InterruptedSwing extends JFrame
{
Thread thread;
public InterruptedSwing() {
super();
final JProgressBar progressBar=new JProgressBar();//进度条组件
getContentPane().add(progressBar,BorderLayout.NORTH);
progressBar.setStringPainted(true);
thread=new Thread(new Runnable() {
int count=0;
public void run() {
while(true)
{
progressBar.setValue(++count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("当前线程被中断");
break;
}
} }
});
thread.start();
thread.interrupt();//中断线程
}
public static void init(JFrame frame,int width,int height)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width,height);
frame.setVisible(true);
}
public static void main(String[] args)
{
init(new InterruptedSwing(), 100, 200); } }
线程--demo3的更多相关文章
- [高并发]Java高并发编程系列开山篇--线程实现
Java是最早开始有并发的语言之一,再过去传统多任务的模式下,人们发现很难解决一些更为复杂的问题,这个时候我们就有了并发. 引用 多线程比多任务更加有挑战.多线程是在同一个程序内部并行执行,因此会对相 ...
- java线程跟多线程
java创建线程两种方式: 1.继承Thread创建线程 /** * Created by lsf on 16/4/18. */ class NewThread extends Thread { Ne ...
- Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...
- java多线程系类:基础篇:09之interrupt()和线程终止方式
概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...
- 06_Java多线程、线程间通信
1. 线程的概念 1.1多进程与多线程 进程:一个正在执行的程序.每个进程执行都有一个执行顺序,该顺序是一个执行路径,或叫一个控制单元. 一个进程至少有一个线程. 线程:就是进程中的一个独立 ...
- 基础学习day12--多线程一线程之间的通信和常用方法
一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信. 等待/唤醒机制涉及的方法: 1. wait():让线程处于冻结状态,被wa ...
- 04_线程的创建和启动_使用Callable和Future的方式
[简述] 从java5开始,java提供了Callable接口,这个接口可以是Runnable接口的增强版, Callable接口提供了一个call()方法作为线程执行体,call()方法比run() ...
- java--多线程之前台幕后
前台程序是相对于后台程序来说的,那么什么是后台程序呢? [后台程序]就是在启动了start()之前,调用了setDaemon(true)方法,这个线程就变成了后台.如果一个进程中只用后台线程在运行,那 ...
- Java多线程(九)—— interrupt()和线程终止方式
一.interrupt() 说明 interrupt()的作用是中断本线程.本线程中断自己是被允许的:其它线程调用本线程的interrupt()方法时,会通过checkAccess()检查权限.这有可 ...
随机推荐
- mysql 出现Host 'localhost' is not allowed to connect to this MySQL server 错误
MySql数据库:Host 'localhost' is not allowed to connect to this MySQL server 修改mysql的root密码后,出现Host 'loc ...
- Java泛型中的标记符含义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...
- C++中GB2312字符串和UTF-8之间的转换
在编程过程中需要对字符串进行不同的转换,特别是Gb2312和Utf-8直接的转换.在几个开源的魔兽私服中,很多都是老外开发的,而暴雪为了能 够兼容世界上的各个字符集也使用了UTF-8.在中国使用VS( ...
- javaweb图片上传 tomcat重新部署 图片消失
标签: 图片上传tomcat重新部署图片消失原因分析以及解决办法 最近在做一个Javaweb的项目,涉及到图片上传,并且需要将图片通过URL回显给JSP页面,在调试的时候发现,上传到tomcat的 ...
- Ajax-05 使用XMLHttpRequest和jQuery实现Ajax实例
需求: (django)使用XMLHttpRequest和jQuery实现Ajax加法运算 url.py: from django.conf.urls import url from hello im ...
- 29-THREE.JS 根据公式画形状
<!DOCTYPE html> <html> <head> <title></title> <script src="htt ...
- MySQL 5.7.3.0 安装 全程截图
前言: 下一个班快讲MySQL数据库了,正好把服务器里面的MySQL卸了重装了一下. 截个图,作为笔记.也正好留给需要的朋友们. 目录: 下载软件 运行安装程序 安装程序欢迎界面 许可协议 查找更新 ...
- 图解 ASP.NET Core开发环境准备
2016年6月28日微软宣布发布 .NET Core 1.0.ASP.NET Core 1.0 和 Entity Framework Core 1.0. .NET Core是微软在两年前发起的开源跨平 ...
- 任务调度 Spring Task 4(二 )
注解和配置文件两种 第一种:配置文件方式 第一步:编写作业类 即普通的pojo,如下: import org.springframework.stereotype.Service; @Service ...
- Nginx 日志分析及性能排查
Nginx 日志分析及性能排查 2017-03-04 Linux爱好者 (点击上方公众号,可快速关注) 作者:-外星人- my.oschina.net/362228416/blog/844713 如有 ...