示例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. Learining TypeScript (一) TypeScript 简介

    Learining TypeScript (一) TypeScript 简介 一.TypeScript出现的背景    2 二.TypeScript的架构    2 1.    设计目标    2 2 ...

  2. discuz! 设置私密论坛版块的方法

    Discuz!的强大功能不用细说, 话说对于有一部分需要设置具有一定访问权限的用户才能浏览的版块内容的话. 可能很多朋友不太清楚, 为了解决这个问题, 第一步以管理员的身份登陆, 然后 论坛-> ...

  3. react-webpack(二)

    const path = require('path'); const webpack = require('webpack'); const HtmlWebpackPlugin = require( ...

  4. 【python】命令行解析工具argparse用法

    python的命令行参数 之前有用到optget, optparse, 现在这些都被弃用了. import argparse parser = argparse.ArgumentParser() ar ...

  5. Django -- DRF 认证流程

    Django Restful Framework (DRF)中类的调用与自定义-- 以 autentication 认证为例 DRF 的 request 对 django 的 request 进行了更 ...

  6. 【PL/SQL编程】SQL与PL/SQL的区别

    SQL概念: SQL是结构化查询语言,由数据定义语言.数据操纵语言.数据控制语言构成,它不面向过程,即前一条语句与后一条语句无关.它没有流程控制,也不存在变量. PL SQL概念:    PL/SQL ...

  7. 《Drools7.0.0.Final规则引擎教程》第2章 追溯Drools5的使用

    2.1 Drools5简述 上面已经提到Drools是通过规则编译.规则收集和规则的执行来实现具体功能的.Drools5提供了以下主要实现API: KnowledgeBuilder Knowledge ...

  8. 快排的python实现

    快排的python实现 #python 2.7 def quick_sort(L): if len(L) <= 1: return L else: return quick_sort([lt f ...

  9. tab显示不同数据

    效果 核心代码 [js] [#escape x as (x)!?html]<!doctype html><html lang="zh-CN"><hea ...

  10. 转载(原标题:网站再遭新威胁 Struts2又曝高危漏洞啦)

    自从著名J2EE框架Apache Struts2被曝出可被远程攻击者利用的执行漏洞后,关于Struts2的安全性便广受关注.近日,安全研究人员则再次发现了Struts2存在远程代码执行的漏洞,Stru ...