线程--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()检查权限.这有可 ...
随机推荐
- C++(十七) — 宏代码、内联函数
1.C++ 表达式返回值 返回引用:当函数返回引用类型时,没有复制返回值.相反,返回的是对象本身.(与之对应的C语言中,返回的是变量的值) C++中,表达式返回的是变量本身(也就是变量对应的地址). ...
- bash echo color
原文:https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux $ ; i ...
- linux Centos7 下vsftpd 安装与配 FTP
一.说明 linux 系统下常用的FTP 是vsftp, 即Very Security File Transfer Protocol. 还有一个是proftp(Profession ftp). 我们这 ...
- python 在头文件添加 #include \"stdafx.h\"\r\n
import osimport shutil#-*- coding:cp936 -*-import codecsfrom sys import argv def replace_all_files(p ...
- HAWQ取代传统数仓实践(十三)——事实表技术之周期快照
一.周期快照简介 周期快照事实表中的每行汇总了发生在某一标准周期,如一天.一周或一月的多个度量.其粒度是周期性的时间段,而不是单个事务.周期快照事实表通常包含许多数据的总计,因为任何与事实表时间范围一 ...
- 随机获取图片的api接口
http://lorempixel.com/1600/900 https://unsplash.it/1600/900?random(国内加载略慢) https://uploadbeta.com/ap ...
- (一)mvc与mvvm设计模式
前沿:了解设计模式对我们而言,具有很大意义,对语言没有限制,它适用于任何语言,是一种变成思想.设计模式最初有四人帮提出,有兴趣的同学可以去了解下,今天给大家主要分析mvc与mvvm设计模式 一.mvc ...
- DevExpress使用笔记
DevExpress这套控件用了也有几年的时间了,现在用的版本是14.2.2. 确实是很强大的一套控件,用的好,做出的效果也是相当的好,记录一些平常用到的代码,就当记笔记吧,慢慢完善,其实网上也有大把 ...
- [css小技巧]input去除边框问题
border:none;是不够的 (1)在谷歌浏览器添加 outline: none;去除点击后产生的边框; (2)IE7下border: none;还会有边框存在,改用border: 0;即可,同时 ...
- BJOI2019 游记
BJOI 2019 游记 Day 1 开场拿到 \(T1\) 发现可以转成求平均 \(log\) 直接 \(AC\) 自动机上 \(Dp\) 一波即可 \(T2\) 发现是到数论神仙题,大概能想到要用 ...