package me.luger.thread;public class SynchronizedTest { public static void main(String[] args) {
Synchronized1 s1 = new Synchronized1("s1");
Synchronized1 s2 = new Synchronized1("s2");
s1.start();
s2.start();
}
}class Synchronized1 extends Thread{
Synchronized1(String name){
super(name);
} public void run() {
m1();
}
synchronized void m1(){
for(int i = 0;i<1000;i++){
System.out.println(getName()+"------m1--------");
}
}
}
这个是我写的线程锁 我想测试的是“被锁定指的是 其他线程不能访问这个方法”但是这个好像看不出结果来 是不是我例子写的不对 哪位大神给一个能测试上面这句话的例子
对了还有一个问题 被锁定是指其他线程不能访问这个方法 还是不能访问这个类的所有带锁的方法
Synchronized1 s1 = new Synchronized1("s1");
Synchronized1 s2 = new Synchronized1("s2");
s1.start();
s2.start();
}
}class Synchronized1 extends Thread{
Synchronized1(String name){
super(name);
} public void run() {
m1();
}
synchronized void m1(){
for(int i = 0;i<1000;i++){
System.out.println(getName()+"------m1--------");
}
}
}
这个是我写的线程锁 我想测试的是“被锁定指的是 其他线程不能访问这个方法”但是这个好像看不出结果来 是不是我例子写的不对 哪位大神给一个能测试上面这句话的例子
对了还有一个问题 被锁定是指其他线程不能访问这个方法 还是不能访问这个类的所有带锁的方法
解决方案 »
- Jbulider 连不上 SQL SEVER2000
- 设备不可用
- 请问在java api中的这个树形结构是什么意思?
- 请问在苹果的OSX系统上也能够安装JDK1.4以上的版本吗?请高手指教
- 下面有代码有三个错误,帮我看一下。
- 菜鸟问题之3!快回答,快给分!
- 有关Java界面的简单问题(在线等待,高分相送,答者有分)
- 类型转换
- 急急,,jbuilder7里生成的UML怎样能把它导入ROSE呢,或者能否与ROSE连接起来?
- java.lang.NoClassDefFoundError,请问如何解决
- 【开发的小疑问】准备开发一个单机版的软件,框架可以用ssh2开发么?
- 使用JTable做表格的时候,为什么会这样啊,为什么值传不过去?
synchronized void m1(){
修改为:
static synchronized void m1(){
就可以看到效果了。因为静态函数是类级别互斥访问的。
package me.luger.thread;public class SynchronizedTest { public static void main(String[] args) {
Synchronized1 s1 = new Synchronized1("s1");
//Synchronized1 s2 = new Synchronized1("s2");
new Thread(s1).start();
new Thread(s1).start();
}
}class Synchronized1 extends Thread{
Synchronized1(String name){
super(name);
} public void run() {
m1();
}
synchronized void m1(){
for(int i = 0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"------m1--------");
}
}
}
{
public static void main(String[] args)
{
Test test = new Test();
new T1(test).start();
new T2(test).start();
}
}class T1 extends Thread
{
private Test test;
T1(Test test)
{
this.test = test;
}
@Override
public void run()
{
for(int i = 0; i < 10; i ++)
test.test();
}
}class T2 extends Thread
{
private Test test;
T2(Test test)
{
this.test = test;
}
@Override
public void run()
{
for(int i = 0; i < 10; i ++)
test.test();
}
}class Test
{
synchronized public void test()
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " hello");
}
}
{
public static void main(String[] args)
{
D d = new D();
new MyThread(d).start();
new MyThread(d).start();
}
}
class MyThread extends Thread
{
private D d; public MyThread(D d)
{
this.d = d;
} @Override
public void run()
{
d.test();
}
}
class D
{
synchronized public void test()
{
for(int i = 0 ; i < 10; i ++)
{
try
{
Thread.sleep((long)(Math.random() * 500));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " hello");
}
}
}