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=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();
}
}