public class ThreadTest extends Thread {
private int count = 10;
@Override
public void run() {
super.run();
while (true){
System.out.println(count);
if(--count == 0){
return;
}
}
}
public static void main(String[] args) {
new ThreadTest().start();
}
}
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class SwingAndThread extends JFrame {
private JLabel jl = new JLabel();
private static Thread t;
private int count = 0;
private Container container = getContentPane();
public SwingAndThread(){
setBounds(300,200,250,100);
container.setLayout(null);
URL url = SwingAndThread.class.getResource("./WechatIMG2.png");
Icon icon = new ImageIcon(url);
jl.setIcon(icon);
jl.setHorizontalAlignment(SwingConstants.LEFT);
jl.setBounds(10,10,200,50);
jl.setOpaque(true);
t = new Thread(new Runnable() {
@Override
public void run() {
while (count<=200){
jl.setBounds(count,10,200,50);
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
count += 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();
}
}
public class RunnableTest {
private static int count = 10;
private static int count2 = 15;
public static void main(String[] args) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
while (true){
System.out.println(count);
if(--count == 0){
return;
}
}
}
});
t.start();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println(count2);
if(--count2 == 0){
return;
}
}
}
});
t1.start();
}
}
import javax.swing.*;
import java.awt.*;
public class InterruptedSwing extends JFrame {
Thread thread;
public static void main(String[] args) {
init(new InterruptedSwing(),100,100);
}
public static void init(JFrame jf,int width,int height){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(width,height);
jf.setVisible(true);
}
public InterruptedSwing(){
super();
final JProgressBar progressBar = new JProgressBar();
getContentPane().add(progressBar, BorderLayout.NORTH);
progressBar.setStringPainted(true);
thread = new Thread(new Runnable() {
int count = 0;
@Override
public void run() {
while (true){
progressBar.setValue(++count);
try{
thread.sleep(1000);
}catch (InterruptedException e){
System.out.println("当前线程被中断");
break;
}
}
}
});
thread.start();
thread.interrupt();
}
}
import javax.swing.*;
import java.awt.*;
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 static void main(String[] args) {
init(new JoinTest(),100,100);
}
public static void init(JFrame jf,int width,int height){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(width,height);
jf.setVisible(true);
}
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;
@Override
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;
@Override
public void run() {
while (true){
progressBar2.setValue(++count);
try{
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
if(count==100){
break;
}
}
}
});
threadB.start();
}
}
import javax.swing.*;
import java.awt.*;
public class PriorityTest extends JFrame {
Thread threadA;
Thread threadB;
Thread threadC;
Thread threadD;
final JProgressBar progressBar = new JProgressBar();
final JProgressBar progressBar2 = new JProgressBar();
final JProgressBar progressBar3 = new JProgressBar();
final JProgressBar progressBar4 = new JProgressBar();
public static void main(String[] args) {
init(new PriorityTest(),100,100);
}
public static void init(JFrame jf,int width,int height){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(width,height);
jf.setVisible(true);
}
public PriorityTest(){
super();
getContentPane().add(progressBar, BorderLayout.NORTH);
progressBar.setStringPainted(true);
getContentPane().add(progressBar2, BorderLayout.SOUTH);
progressBar2.setStringPainted(true);
getContentPane().add(progressBar3, BorderLayout.WEST);
progressBar3.setStringPainted(true);
getContentPane().add(progressBar4, BorderLayout.EAST);
progressBar4.setStringPainted(true);
threadA = new Thread(new MyThread(progressBar));
threadB = new Thread(new MyThread(progressBar2));
threadC = new Thread(new MyThread(progressBar3));
threadD = new Thread(new MyThread(progressBar4));
setPriority("threadA",5,threadA);
setPriority("threadB",5,threadB);
setPriority("threadC",4,threadC);
setPriority("threadD",3,threadD);
}
public static void setPriority(String threadName,int priority,Thread t){
t.setPriority(priority);
t.setName(threadName);
t.start();
}
private final class MyThread implements Runnable {
private final JProgressBar bar;
int count = 0;
private MyThread(JProgressBar bar){
this.bar = bar;
}
public void run(){
while (true){
bar.setValue(count+=10);
try{
Thread.sleep(1000);
}catch (InterruptedException e){
System.out.println("当前线程被中断");
}
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class SleepMethodTest extends JFrame {
private Thread t;
private static Color[] colors = {
Color.BLACK,
Color.BLUE,
Color.CYAN,
Color.GREEN,
Color.ORANGE,
Color.YELLOW,
Color.RED,
Color.PINK,
Color.LIGHT_GRAY
};
private static final Random random = new Random();
private static Color getColors() {
return colors[random.nextInt(colors.length)];
}
public SleepMethodTest(){
t = new Thread(new Runnable() {
int x = 30;
int y = 50;
@Override
public void run() {
while (true){
try{
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
Graphics graphics = getGraphics();
graphics.setColor(getColors());
graphics.drawLine(x,y,100,y++);
if(y>=80){
y=50;
}
}
}
});
t.start();
}
public static void main(String[] args) {
init(new SleepMethodTest(),100,100);
}
public static void init(JFrame jf,int width,int height){
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(width,height);
jf.setVisible(true);
}
}
public class ThreadSafeTest implements Runnable {
int num = 10;
@Override
public void run() {
while (true){
if(num>0){
try {
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("tickets"+num--);
}
}
}
public static void main(String[] args) {
ThreadSafeTest t = new ThreadSafeTest();
Thread ta = new Thread(t);
Thread tb = new Thread(t);
Thread tc = new Thread(t);
Thread td = new Thread(t);
ta.start();
tb.start();
tc.start();
td.start();
}
}
public class ThreadSafeTest2 implements Runnable {
int num = 10;
@Override
public void run() {
while (true){
synchronized (""){
if(num>0){
try {
Thread.sleep(100);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("tickets"+num--);
}
}
}
}
public static void main(String[] args) {
ThreadSafeTest2 t = new ThreadSafeTest2();
Thread ta = new Thread(t);
Thread tb = new Thread(t);
Thread tc = new Thread(t);
Thread td = new Thread(t);
ta.start();
tb.start();
tc.start();
td.start();
}
}