示例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的更多相关文章

  1. [高并发]Java高并发编程系列开山篇--线程实现

    Java是最早开始有并发的语言之一,再过去传统多任务的模式下,人们发现很难解决一些更为复杂的问题,这个时候我们就有了并发. 引用 多线程比多任务更加有挑战.多线程是在同一个程序内部并行执行,因此会对相 ...

  2. java线程跟多线程

    java创建线程两种方式: 1.继承Thread创建线程 /** * Created by lsf on 16/4/18. */ class NewThread extends Thread { Ne ...

  3. Java多线程系列--“基础篇”09之 interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于“阻塞状态”的线程2.2 终止处于“运行状态” ...

  4. java多线程系类:基础篇:09之interrupt()和线程终止方式

    概要 本章,会对线程的interrupt()中断和终止方式进行介绍.涉及到的内容包括:1. interrupt()说明2. 终止线程的方式2.1 终止处于"阻塞状态"的线程2.2 ...

  5. 06_Java多线程、线程间通信

    1. 线程的概念      1.1多进程与多线程 进程:一个正在执行的程序.每个进程执行都有一个执行顺序,该顺序是一个执行路径,或叫一个控制单元. 一个进程至少有一个线程. 线程:就是进程中的一个独立 ...

  6. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  7. 04_线程的创建和启动_使用Callable和Future的方式

    [简述] 从java5开始,java提供了Callable接口,这个接口可以是Runnable接口的增强版, Callable接口提供了一个call()方法作为线程执行体,call()方法比run() ...

  8. java--多线程之前台幕后

    前台程序是相对于后台程序来说的,那么什么是后台程序呢? [后台程序]就是在启动了start()之前,调用了setDaemon(true)方法,这个线程就变成了后台.如果一个进程中只用后台线程在运行,那 ...

  9. Java多线程(九)—— interrupt()和线程终止方式

    一.interrupt() 说明 interrupt()的作用是中断本线程.本线程中断自己是被允许的:其它线程调用本线程的interrupt()方法时,会通过checkAccess()检查权限.这有可 ...

随机推荐

  1. HBase Cassandra比较

    转自:http://itindex.net/detail/22338-cassandra-hbase-%E8%AE%BE%E8%AE%A1     Cassandra HBase 一致性 Quorum ...

  2. 【lightoj-1025】The Specials Menu(区间DP)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1025 [题目大意] 求一个字符串删去任意字符可以构成多少个不同的回文串 [分析 ...

  3. 修改MAC过程

    首先打开PC的Telnet功能,如下: 对PC设置本地IP 2.cmd→telnet 192.168.1.230(出厂默认IP) 3.root →密码:20...................(公司 ...

  4. ss-libev 源码解析local篇(1): ss_local的启动,客户端连入

    学习研究ss-libev的一点记录(基于版本3.0.6) ss_local主要代码在local.c中,如果作为一个库编译,可通过start_ss_local_server启动local server. ...

  5. asp.net viewstate 数据大导致错误

    当在ViewState中放入dataSet的数据量比较大的时候,当再点页面上的控件时,不会返回到后台,并且会出现如下错误: 或者是上面的12030改成500的错误. --解决方法:Viewstate绑 ...

  6. Windows GVLK密钥对照表(KMS激活专用

    以下key来源于微软官网:https://technet.microsoft.com/en-us/library/jj612867.aspx Windows Server 2016 操作系统 KMS激 ...

  7. Linux系统中的wc

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...

  8. SQLServer清空数据库中所有表的数据

    今早同事跟进客户反馈的问题时,提了个要求,要求清空数据库中所有表的数据. 记得之前用游标遍历所有的表名 + exec 动态语句 truncate table 表名 实现过这个功能. 网上搜了下,有更简 ...

  9. 剑指offer-第四章解决面试题思路(字符串的排序)

    题目:输入一个字符串,打印出该字符串的全排列. 思路:将整个字符串分成两部分,第一部分为一个字符,将该字符和该字符后面的字符(直到最后一个字符)依次交换,确定第一个字符:然后固定第一个字符,将后面的字 ...

  10. WPF简单模拟QQ登录背景动画(转)

    介绍 之所以说是简单模拟,是因为我不知道QQ登录背景动画是怎么实现的.这里是通过一些办法把它简化了,做成了类似的效果 效果图 大体思路 首先把背景看成是一个4行8列的点的阵距,X轴Y轴都是距离70.把 ...