小编Hak*_*ata的帖子

Hibernate:CascadeType.PERSIST不起作用,但CascadeType.ALL保存对象

@Entity
@Table(name = "Section_INST")
public class Section {

@javax.persistence.Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "Section_ID_GENERATOR")
@SequenceGenerator(name = "Section_ID_GENERATOR",sequenceName = "Section_ID_SEQUENCER" , initialValue = 1 , allocationSize = 1)
@Column(name = "Section_ID")
private int Id;

@Column(name = "Section_Name")
private String name;

@OneToOne(optional = false,cascade = CascadeType.PERSIST)
@JoinColumn(name = "Exch_ID")
private Exchange exchange;

//---Constructor and Getter Setters-----
}


@Entity
@Table(name = "EXCHANGE_INST")
public class Exchange {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "Exchange_ID_GENERATOR")
@SequenceGenerator(name = "Exchange_ID_GENERATOR",sequenceName = "Exchange_ID_SEQUENCER" , initialValue = 1 , allocationSize …
Run Code Online (Sandbox Code Playgroud)

hibernate cascade

7
推荐指数
1
解决办法
6959
查看次数

持有多个锁的线程进入wait()状态.它是否释放所有锁定锁?

我编写了这个程序来检查线程t1是否在两个不同的对象上持有锁:Lock.class和MyThread.class使用MyThread.class.wait()在MyThread.class实例上进入等待模式.它不会释放Lock上的锁.类实例.为什么这样 ?我一直在想,一旦线程进入等待模式或它死了,它就会释放所有获得的锁.

public class Lock {

protected static volatile boolean STOP = true;
public static void main(String[] args) throws InterruptedException {
    MyThread myThread = new MyThread();
    Thread t1 = new Thread(myThread);
    t1.start();
    while(STOP){
    }
    System.out.println("After while loop");
    /*
     * 
     */
    Thread.sleep(1000*60*2);
    /*
     * Main thread should be Blocked.
     */
    System.out.println("now calling Check()-> perhaps i would be blocked. t1 is holding lock on class instance.");
    check();
}

public static synchronized void check(){
    System.out.println("inside Lock.check()");
    String threadName = Thread.currentThread().getName();
    System.out.println("inside Lock.Check() method …
Run Code Online (Sandbox Code Playgroud)

java multithreading

6
推荐指数
1
解决办法
3909
查看次数

添加两个阵列的元素

你有两个数组

int[] a = {......} // Total elements 1 million
int[] b = {......} // Total elements 1 million , Length is same for both arrays.
Run Code Online (Sandbox Code Playgroud)

Q1.我必须创建一个数组

int[] c 
Run Code Online (Sandbox Code Playgroud)

其元素是a []和b [] Ex的相应索引的总和.

 c[0] = a[0] + b[0];
 c[1] = a[1] + b[1];
 c[2] = a[2] + b[2];
Run Code Online (Sandbox Code Playgroud)

解决方案: - >我可以利用多线程.将整个数组划分为10个或更多部分,并将每个段分配给线程以执行计算.注意 - >访员建议使用多线程

Q2.现在它有点改变.数组C的元素将具有如下值: - >

c[0] = a[0] + b[0];
c[1] = a[1] + b[1] + c[0]; // At this line c[0] is Sum of a[0] + b[0]; The …
Run Code Online (Sandbox Code Playgroud)

language-agnostic arrays algorithm parallel-processing multithreading

6
推荐指数
2
解决办法
1438
查看次数

文件写入:字符串到字节

我想在文件中将字符串/字符数据写为字节.我希望这种转换在IO.*类内部发生.我不想在字符串上使用getBytes()方法.

我尝试了两个程序,但都将数据写为字符.当我在记事本中打开文件时,我可以阅读这些字符.如何将数据存储为字节?

     import IO.FileWrite;

         import java.io.*;

        public class CharToChar {

private final String data;

public CharToChar(String data){
    this.data = data;
}

public static void main(String[] args) throws IOException {
    final CharToChar charToChar = new CharToChar("I am Manish");
    charToChar.write();
}

private void write() throws IOException {
    final File file = new File("CharToChar.txt");
    final FileWriter fileWriter = new FileWriter(file);
    final BufferedWriter bufferdWriter = new BufferedWriter(fileWriter);
    bufferdWriter.write(this.data);
    bufferdWriter.close();

}
  }


     import java.io.DataOutputStream;
     import java.io.FileOutputStream;
       import java.io.IOException;

       public class WriteStringAsBytesToFile {

public static void …
Run Code Online (Sandbox Code Playgroud)

java file-io file java-io

1
推荐指数
1
解决办法
9787
查看次数

在java 1.5中:可以实现java.lang.Runnable接口的run()方法有一个非void的返回类型吗?

我今天出席了采访.面试官让我在java 1.5中编写一个线程.你可以扩展java.lang.thread类或实现java.lang.Runnable接口.是否有可能实现的run()方法具有void之外的返回类型.Runnable接口中的方法签名是

public void run();
Run Code Online (Sandbox Code Playgroud)

我的回答是"由于上面的签名定义,你无法从run()方法返回一个int或字符串."

面试官在java 1.5中说你可以从run方法返回int或string.

你能告诉我怎么做吗?

我尝试下面给出了编译错误:

public class SampleInterview implements Runnable {
    @Override
    public int run() {
        System.out.println("Start : inside run of SampleInterview");
        System.out.println("End : inside run of SampleInterview");
    }
}
Run Code Online (Sandbox Code Playgroud)

我搜索谷歌也找不到任何这样的方式.有可能吗?

java multithreading

0
推荐指数
1
解决办法
1616
查看次数