Click here to Skip to main content
16,019,983 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
package threading;

import javax.swing.JLabel;


public class Tabel extends javax.swing.JFrame {

/** Creates new form Tabel */
public Tabel() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText("jLabel1");

jLabel2.setText("jLabel2");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(170, 170, 170)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(126, 126, 126)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(112, 112, 112)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(136, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(149, 149, 149)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(207, Short.MAX_VALUE))
);

pack();
}//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here

Syncrodemo obj=new Syncrodemo(jLabel1,jLabel2);
Thread11 obj1=new Thread11(obj);
obj1.start();
Thread12 obj2=new Thread12(obj);
obj2.start();

}
class Syncrodemo
{
int num;
JLabel lbl;
JLabel lbl1;

Syncrodemo(JLabel lbl,JLabel lbl1)
{
this.lbl=lbl;
this.lbl1=lbl1;
}
void printtable(int num,JLabel lbl)
{
synchronized(this)
{

this.num=num;
for(int i=1;i<=10;i++)
{
try{




jLabel1.setText(""+i*num);

jLabel2.setText(""+i*num);
wait(1000);
}
catch(Exception ex)
{}
}
}
}}
class Thread11 extends Thread
{


Syncrodemo t;
public Thread11(Syncrodemo t)
{
this.t=t;
}
public void run()
{
t.printtable(5,jLabel1);
}
}
class Thread12 extends Thread
{
Syncrodemo t;
public Thread12(Syncrodemo t)
{
this.t=t;
}
public void run()
{
t.printtable(8,jLabel2);
}
}


/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Tabel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
// End of variables declaration
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900